Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I select only the first following sibling?

Tags:

xpath

Using XPATH, I am trying to get just the ISBN number out of the following:

<span class="product_info_details">
   <b class="">ISBN: </b>1941529429
   <b class="">Contributors: </b> (Illustrator)
   <b class="">Publisher:</b> Parallax Pr
   <b class="">Published:</b> Nov 1 2016
</span>

Now, when I use:

//span[@class="product_info_details"]/b/following-sibling::text()

I get the output:

1941529429

(Illustrator)

Parallax Pr

Nov 1 2016

What can I use so that I just get the first value (1941529429) as the result?

like image 620
UltimatePeter Avatar asked Oct 18 '25 14:10

UltimatePeter


1 Answers

You want to limit b element that the XPath find to the one that contains substring 'ISBN', and limit the following sibling text node to the nearest only :

//span[@class="product_info_details"]/b[contains(.,'ISBN')]/following-sibling::text()[1]

Alternatively, you can limit b element that the XPath find to the one that starts with substring 'ISBN' :

..../b[starts-with(.,'ISBN')]/....

Using position index 1, as mentioned on the other answer, should also work given the order of the b elements is consistent, but the two alternatives given above represents your intention better as it mentioned the substring 'ISBN' on which you based your search criteria to find the target text.

like image 81
har07 Avatar answered Oct 22 '25 01:10

har07