I started to learn xpath at w3cschool. I am trying to evaluate the number of players in the game. (so counting the screenname and the text() elements/attributes)
Here is my example xml:
<game>
<info>
<name>My Game</name>
<description>A <i>very</i> interesting game.</description>
<started>2012-03-01T18:00:00Z</started>
<players number="2">
<screenname player="1">Alice</screenname>
<screenname player="2">Bob</screenname>
</players>
<rounds>2</rounds>
<winner player="2"/>
</info>
</game>
the xpath query I use is:
//game/info/players[count(.//screenname/text())]
But I only get:
"Result is a NodeSet containing 1 element"
(tool: http://www.whitebeam.org/library/guide/TechNotes/xpathtestbed.rhtm)
What causes this error?
Your XPath query should probably be:
count(/game/info/players/screenname)
The XPath you are using now is asking for all game/info/players
nodes which have a non-zero number of screenname/text()
descendent nodes. Since there's only one players
element, the resulting nodeset contains only one element.
Remember, an XPath is a series of axis-nodetest-predicate expressions (axis::nodetest[predicate]
) separated by steps (/
). Only axis-nodetest is part of the result set sent to the next step--the predicate never appears in a result set because it is only used to filter.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With