findvalue('//a[1]'); I use HTML::TreeBuilder::XPathin perl. Now...">

Why does the following XPath statement return all "a" elements?

print $tree->findvalue('//a[1]');

I use HTML::TreeBuilder::XPathin perl. Now I expect the above statute to return the value of the second element “a”, but instead will return the value of all “a” elements on the page. I can’t understand why?

+3
source share
2 answers

what you have should return the a-child of each element first.
therefore, it //a[1]will work as follows (the result will be 2 nodes):

X
 Y
  a <-- give you this
  a
Z
 a <-- and this
 a

try (//a)[1]instead

+8
source

XPATH a , a .

, , XML, a ( a, , ).

, a , : (//a)[1]

//a , -, , a, //.

+3

Source: https://habr.com/ru/post/1760460/


All Articles