all groups > dotnet xml > june 2006 >
You're in the

dotnet xml

group:

Use of count() function with XPath


Use of count() function with XPath Mario Vázquez
6/2/2006 12:00:00 AM
dotnet xml:
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?

Re: Use of count() function with XPath Martin Honnen
6/2/2006 1:49:16 PM


[quoted text, click to view]


[quoted text, click to view]


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
Re: Use of count() function with XPath Mario Vázquez
6/2/2006 5:04:50 PM
Ok, that's right, but there is any way to do this *ONLY* with a XPath
expresion?
: )

"Martin Honnen" <mahotrash@yahoo.de> escribió en el mensaje
news:e2UDWqjhGHA.4080@TK2MSFTNGP03.phx.gbl...
[quoted text, click to view]

Re: Use of count() function with XPath Martin Honnen
6/2/2006 5:18:52 PM


[quoted text, click to view]

Yes, as I said:

[quoted text, click to view]

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

--

Martin Honnen --- MVP XML
Re: Use of count() function with XPath Mario Vázquez
6/2/2006 6:01:31 PM
: /
Ok, thanks


"Martin Honnen" <mahotrash@yahoo.de> escribió en el mensaje
news:e07idflhGHA.4304@TK2MSFTNGP05.phx.gbl...
[quoted text, click to view]

Re: Use of count() function with XPath Peter Flynn
6/3/2006 12:34:36 AM
[quoted text, click to view]

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/
Re: Use of count() function with XPath Michael Mooney
6/14/2006 2:42:55 PM

[quoted text, click to view]

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.

AddThis Social Bookmark Button