Instead of
"//div[contains(@id,'art')][1]"
you want
"(//div[contains(@id,'art')])[1]"
The reason has to do with binding precedence. As you probably know, [1]
is shorthand for [position() = 1]
. In the variant that you were trying to use, this means "when the current node (the div
element) is the first child of its parent". Clearly, there could be many such divs that are each the first child of their respective parent.
When you put parentheses around the expression //div[predicate]
and append [1]
to that, then you're asking the question you intended to ask: what is the first node in the nodeset selected by //div[predicate]
?