tags:

views:

48

answers:

2
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?

+8  A: 

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

+1 Nice visualization to explain and a succinct explanation.
Mads Hansen
[Xacobeo](http://p3rl.org/Xacobeo) can do this visualisation in an interactive manner.
daxim
+3  A: 

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 //.

Mads Hansen