I am trying to select the first element in a set of resulting nodes after executing an xpath query.
When I do this:
//dl
I get the following result set:
[<dl>…</dl>, <dl>…</dl>]
How can I get the first one? Neither of these work:
//dl[1]
//dl[position()=1]
I am executing this in Chrome’s Web Inspector.
Use the following:
The parentheses are significant. You want the first node that results from
//dl(not the set ofdlelements that are the first child of their parent (which is what//dl[1](no parens) returns)).This is easier to see when one realizes that
//is shorthand for (i.e. expands fully to)/descendant-or-self::node()/so that//dl[1]is equivalent to:…which is more obviously not what you want. Instead, you’re looking for: