Drew
Drew

Reputation: 2663

XPath returning unexpected elements

So I have a document with multiple divs.

using the path '//div[1]' seems? to be returning multiple elements.

thoughts?

Upvotes: 0

Views: 72

Answers (1)

Ian Roberts
Ian Roberts

Reputation: 122394

// is a shorthand in XPath for /descendant-or-self::node()/ including the leading and trailing slashes. So //div[1] fully expanded means

/descendant-or-self::node()/child::div[1]

i.e. every div element anywhere in the document that is the first div child of its respective parent.

If you want just the first div in the whole document then you need parentheses:

(//div)[1]

or use the descendant:: axis explicitly

/descendant::div[1]

Upvotes: 2

Related Questions