Title

Some stuff

other...">

XPath for all nested text except the text "n" nested tags

I have the following html.

<div id="content"> <h3>Title</h3> <p>Some stuff</p> <p>other stuff</p> <p>other other stuff</p> <p>unnecessary stuff</p> <p>other unnecessary stuff</p> </div> 

I have written this expression so far.

 //div[@id="content"]//text() 

Which works, but I want not to extract text from the last 2 <p> elements, because this is not necessary. I tried to write this ...

 //div[@id="content"]/p[not(position() > last() - 2)]//text() 

Which does not work properly. Then I tried this ...

 //div[@id="content"]/[not(self::p[position() > last() - 2])]//text() 

Which didn't work either.

0
source share
1 answer

This expression returns the text nodes you are interested in:

 //div[@id="content"]/*[not(self::p and position() > last() - 2)]//text() 

You have lost * after / .

+2
source

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


All Articles