I have the following XML:
<parent>
<pet>
<data>
<birthday/>
</data>
</pet>
<pet>
<data>
<birthday/>
</data>
</pet>
</parent>
And now I want to select the first birthday element via parent//birthday[1]
but this returns both birthday elements because bothof them are the first child of their parents.
How can I only select the first birthday element of the entire document no matter where it is located. I've tried parent//birthday[position()=1]
but that doesn't work either.
The key part of this XPath is *[1] , which will select the node value of the first child of Department . If you need the name of the node, then you will want to use this... Save this answer.
For example if both text fields have //input[@id='something'] then you can edit the first field xpath as (//input[@id='something'])[1] and the second field's xpath as (//input[@id='something'])[2] in object repository.
You need to wrap the expression that selects the a in parenthesis to group them in a node-set, then apply the predicate filter on position. That will find all a elements except the first one (since there is no a preceding the first one).
node() matches any node (the least specific node test of them all) text() matches text nodes only. comment() matches comment nodes. * matches any element node.
You mean (note the parentheses!)
(/parent/pet/data/birthday)[1]
or, a shorter, but less specific variation:
(/*/*/*/birthday)[1] (//birthday)[1]
or, more semantic, the "birthday of the first pet":
/parent/pet[1]/data/birthday
or, if not all pets have birthday entries, the "birthday of the first pet that for which a birthday is set":
/parent/pet[data/birthday][1]/data/birthday
If you work from a context node, you can abbreviate the expression by making it relative to that context node.
Explanation:
/parent/pet/data/birthday[1]
selects all <birthday>
nodes that are the first in their respective parents (the <data>
nodes), throughout the document(/parent/pet/data/birthday)[1]
selects all <birthday>
nodes, and of those (that's what the parentheses do, they create an intermediary node-set), it takes the first oneIf 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