473,378 Members | 1,482 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Use of count() function with XPath

Hi,

I'm tryin in vane to get the number of elements of some type from a XML
document using the count() function.
I'm not in a XSLT document. I'm just trying to get this from a .NET
XmlDocument object.
How can I obtain the number of ROOMs of an offer?
....
<ROOT>
<OFFER ID="219" DURATION="8" IMAGE="CZG2.jpg">
<ROOM PRICE="468.00" PARTICIPANTS="3" />
<ROOM PRICE="479.00" PARTICIPANTS="5" />
<ROOM PRICE="505.00" PARTICIPANTS="7" />
</OFFER>
<OFFER ID="187" DURATION="8" IMAGE="CUIXG6.jpg">
<ROOM PRICE="85.00" PARTICIPANTS="1" />

</OFFER>
</ROOT>

I've loaded the document in a XmlDocument

Dim xml As XmlDocument()
Dim nodes As XmlNodeList

xml=new XmlDocument()
xml.Load("fileName.xml")
nodes=xml.SelectNodes("count(//ROOM[@ID='219'])")

Why this does not work?
Jun 2 '06 #1
6 33119


Mario Vázquez wrote:

xml=new XmlDocument()
xml.Load("fileName.xml")
nodes=xml.SelectNodes("count(//ROOM[@ID='219'])")

Why this does not work?

For SelectNodes you need to pass in an XPath expression yielding a
node-set. If you want to count the number of nodes simply do e.g.
xml.SelectNodes("//ROM[@ID = '219']").Count

Or create an XPathNavigator and Evaluate the count expression.
--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Jun 2 '06 #2
Ok, that's right, but there is any way to do this *ONLY* with a XPath
expresion?
: )

"Martin Honnen" <ma*******@yahoo.de> escribió en el mensaje
news:e2**************@TK2MSFTNGP03.phx.gbl...


Mario Vázquez wrote:

xml=new XmlDocument()
xml.Load("fileName.xml")
nodes=xml.SelectNodes("count(//ROOM[@ID='219'])")

Why this does not work?

For SelectNodes you need to pass in an XPath expression yielding a
node-set. If you want to count the number of nodes simply do e.g.
xml.SelectNodes("//ROM[@ID = '219']").Count

Or create an XPathNavigator and Evaluate the count expression.
--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/

Jun 2 '06 #3


Mario Vázquez wrote:
but there is any way to do this *ONLY* with a XPath
expresion?


Yes, as I said:
Or create an XPathNavigator and Evaluate the count expression.


Dim navigator as XPathNavigator = xml.CreateNavigator()
navigator.Evaluate("count(//ROOM[@ID='219'])")

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Jun 2 '06 #4
: /
Ok, thanks
"Martin Honnen" <ma*******@yahoo.de> escribió en el mensaje
news:e0**************@TK2MSFTNGP05.phx.gbl...


Mario Vázquez wrote:
but there is any way to do this *ONLY* with a XPath expresion?


Yes, as I said:
Or create an XPathNavigator and Evaluate the count expression.


Dim navigator as XPathNavigator = xml.CreateNavigator()
navigator.Evaluate("count(//ROOM[@ID='219'])")

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/

Jun 2 '06 #5
Mario Vázquez wrote:
Hi,

I'm tryin in vane to get the number of elements of some type from a XML
document using the count() function.
I'm not in a XSLT document. I'm just trying to get this from a .NET
XmlDocument object.
How can I obtain the number of ROOMs of an offer?
...
<ROOT>
<OFFER ID="219" DURATION="8" IMAGE="CZG2.jpg">
<ROOM PRICE="468.00" PARTICIPANTS="3" />
<ROOM PRICE="479.00" PARTICIPANTS="5" />
<ROOM PRICE="505.00" PARTICIPANTS="7" />
</OFFER>
<OFFER ID="187" DURATION="8" IMAGE="CUIXG6.jpg">
<ROOM PRICE="85.00" PARTICIPANTS="1" />

</OFFER>
</ROOT>

I've loaded the document in a XmlDocument

Dim xml As XmlDocument()
Dim nodes As XmlNodeList

xml=new XmlDocument()
xml.Load("fileName.xml")
nodes=xml.SelectNodes("count(//ROOM[@ID='219'])")

Why this does not work?


Because ID is not an attribute on the ROOM element type. It's an
attribute on OFFER. As OFFER is the parent of ROOM, you can write:

count(//ROOM[../@ID='219'])

///Peter
--
XML FAQ: http://xml.silmaril.ie/

Jun 2 '06 #6

"Mario Vázquez" <al*****@microsoft.com> wrote in message
news:u5**************@TK2MSFTNGP02.phx.gbl...
Hi,

I'm tryin in vane to get the number of elements of some type from a XML
document using the count() function.
I'm not in a XSLT document. I'm just trying to get this from a .NET
XmlDocument object.
How can I obtain the number of ROOMs of an offer?
...
<ROOT>
<OFFER ID="219" DURATION="8" IMAGE="CZG2.jpg">
<ROOM PRICE="468.00" PARTICIPANTS="3" />
<ROOM PRICE="479.00" PARTICIPANTS="5" />
<ROOM PRICE="505.00" PARTICIPANTS="7" />
</OFFER>
<OFFER ID="187" DURATION="8" IMAGE="CUIXG6.jpg">
<ROOM PRICE="85.00" PARTICIPANTS="1" />

</OFFER>
</ROOT>

I've loaded the document in a XmlDocument

Dim xml As XmlDocument()
Dim nodes As XmlNodeList

xml=new XmlDocument()
xml.Load("fileName.xml")
nodes=xml.SelectNodes("count(//ROOM[@ID='219'])")

Why this does not work?


It doesn't work because there is no ROOM element that contains an attribute
named "ID".

Something like this should work:
count(//OFFER[@ID='219']/ROOM)

Better yet, save yourself some headaches in the long run (in my opinion) and
remove the "//":
count(/ROOT/OFFER[@ID='219']/ROOM)

And since you are only expecting a single scaler value instead of a node
list, you might as well use SelectSingleNode instead of SelectNodes.
Jun 14 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Scott Castillo | last post by:
The following line prints something: echo $keywords; However, when I then use the count function as follows: $count = count($keywords); echo $count;
1
by: Irfan | last post by:
Hello, I am having some problem with count function. I have a report in which has grouping by a person wise and sub grouping in invoied status e.g.. the output will be Irfan Records (First...
3
by: auron | last post by:
Hi there, I have a really stupid and banal problem with showing the results of a MySQL query in PHP, preciselly with MySQL count() function that gives to a variable in PHP the result. NOTE:...
0
by: DAnne | last post by:
I'm trying to do a simple count function in xslt and it's turning out to be extremely painful. I have a report that brings back a set of responses for each question per section in an Audit. I...
0
by: ambika_se | last post by:
hi all I need a help to use count function. I want to make a query in which i want to search for some keyword so the query i made was for $x in doc("file:///C:/Package1.xml")//Package where...
4
by: Brian P | last post by:
I have generated a number of queries successfully. However, I would in my subsequent reports like to get a count of each field which has data in it (that is, "is not null"). But I am confused...
3
by: atiq | last post by:
I am creating a report where i want to display the total number of students found for that query. i use the following but it displays #Error. =Count() Also on the same report, i have another...
1
by: emorrison | last post by:
I hope this isn't too vague, but I have a large XML file and when I try to use a count function in an XSL file to get the number of nodes, it returns 0 for a particular node. I can correctly count...
16
by: lovecreatesbea... | last post by:
It takes mu so time to finish this C source code line count function. What do you think about it? / ******************************************************************************* * Function ...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.