Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XPath for href link

Tags:

html

href

xml

xpath

I got for example

<a href="target.php">News</a>
<a href="more.php">More</a>

What would be the most efficient way of extracting the href based on the value between <a></a> (the atomic data) with XPath

like image 696
byenary Avatar asked Oct 21 '25 22:10

byenary


1 Answers

Question interpretation:

  • "Most efficient" is taken to mean in a programmer's time sense, not in a performance sense.
  • "The value between" is taken to mean the string between the a tags.

This XPath selects all a elements,

//a

This XPath selects all a elements whose string value is "News":

//a[.='News']

This XPath selects all href attributes of all a elements whose string value is "News"1:

//a[.='News']/@href

1. Credit: @localghost posted correct answer in comments

like image 88
kjhughes Avatar answered Oct 23 '25 13:10

kjhughes