473,241 Members | 1,930 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,241 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 33072


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 ...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

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.