XPath: select the first 5 items

I am currently clearing some data from another website, and I am having problems getting only the first 5 elements.

$travelguide_row = $travelguide_xpath->query('//div[@class="traveltips"]//span|//div[@class="traveltips"]//p'); 

Is it possible to add additional syntaxes after //span and //p ? If so, how?

+5
source share
1 answer

You can use the predicate expression [position() < 6] to achieve this. The following XPath should provide you with the first 5 elements matching your original XPath expression:

 (//div[@class="traveltips"]//span|//div[@class="traveltips"]//p)[position() < 6] 
+6
source

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


All Articles