470,855 Members | 1,140 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,855 developers. It's quick & easy.

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 32423


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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Scott Castillo | last post: by
1 post views Thread by Irfan | last post: by
3 posts views Thread by auron | last post: by
reply views Thread by DAnne | last post: by
reply views Thread by ambika_se | last post: by
4 posts views Thread by Brian P | last post: by
16 posts views Thread by lovecreatesbea... | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.