Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

xpath, count elements error: "Result is a NodeSet containing 1 element"

Tags:

xml

xpath

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?

like image 236
maximus Avatar asked Dec 21 '22 21:12

maximus


1 Answers

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.

like image 182
Francis Avila Avatar answered Jan 01 '23 09:01

Francis Avila