print $tree->findvalue('//a[1]');
I am using HTML::TreeBuilder::XPath
in perl. Now i expect the above statment to return the value of second "a" element but instead it returns the value of all "a" elements in the page. I cant understand Why?
print $tree->findvalue('//a[1]');
I am using HTML::TreeBuilder::XPath
in perl. Now i expect the above statment to return the value of second "a" element but instead it returns the value of all "a" elements in the page. I cant understand Why?
what you have shall return first a-child of every element.
so //a[1]
will work as follows (result will be 2 nodes):
X
Y
a <-- give you this
a
Z
a <-- and this
a
try (//a)[1]
instead
That XPATH expression looks for all a
elements at every level of the document and the predicate filter selects the first a
at every step.
So, depending on how your XML is structured, you might not get every a
element (if there were more than one a
that were siblings, you would only get the first one of those siblings).
However, if you intended to just select the first a
in the document, you could use this expression: (//a)[1]
Wrapping the selection of //a
in the parenthesis creates a collection that the predicate filter is then applied, selecting the first in the collection, rather than the first a
encountered at each step of the //
.