21

I am parsing some XML something like this:

<root>
    <some_gunk/>
    <dupe_node>
        ...
        stuff I want
        ...
    </dupe_node>
    <bits_and_pieces/>
    <other_gunk/>
    <dupe_node>
        ...
        stuff I don't want
        ...
    </dupe_node>
    <more_gunk/>
</root>

An XPath of '//dupe_node' will give me two instances of dupe_node to play with. I only want to traverse the first. Can I do this with XPath?

Mat
  • 82,161
  • 34
  • 89
  • 109

3 Answers3

45
/descendant::dupe_node[1]

//dupe_node[1] is generally wrong, although it produces an identical result in this particular case. See docs:

The location path //para[1] does not mean the same as the location path /descendant::para[1]. The latter selects the first descendant para element; the former selects all descendant para elements that are the first para children of their parents.

Given the following XML:

<foo>
    <bar/>
    <foo>
        <bar/>
    </foo>
</foo>

//bar[1] will produce two nodes, because both bars are first children of their respective parents.

/descendant::bar[1] will give only one node, which is the first of all the bars in the document.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Azat Razetdinov
  • 988
  • 7
  • 7
  • 1
    So how do you write it if you want the first node that is a descendant or self? /descendant::dupe_node[1] excludes self right? – xr280xr Mar 15 '13 at 19:42
7
//dupe_node[1]

XPath counts from 1, not 0 in this case. You can use this tool to try things out in your browser:

http://www.xmlme.com/XpathTool.aspx?mid=82

Simon Willison
  • 15,642
  • 5
  • 36
  • 44
4

//dupe_node[1]

Jonas Elfström
  • 30,834
  • 6
  • 70
  • 106