Xpath selects all text (), but not from a specific tag / class attribute

I want to ask a question:

The structure is similar:

<div class='item'> <div class='all'> BY <p>ABC</p> <p>DEF</p> <p> <span class="date">19991111</span> </p> </div> </div> 

I want to ask how to select all text() (BY ABC DEF), but does not contain <span class="date">DDD</span> (19991111)

I found the not() method span[not(@class, 'date')
but I don’t know how to combine with //div[@class=item]/div[@class='all']//text()

Please Teach Me Thank You

+5
source share
2 answers

You can combine them as follows:

 //div[@class=item]/div[@class='all']//text()[not(parent::span[@class='date'])] 

Above XPath, select all text nodes in the div[@class='all'] whose parent does not match span[@class='date'] .

You can also filter out empty text nodes with normalize-space() :

 //div[@class=item]/div[@class='all']//text()[not(parent::span[@class='date']) and normalize-space()] 
+2
source

Use the descendant-or-self axis with the * selector with the not(self::span and @class='date') predicate not(self::span and @class='date') .

for instance

 //div[@class='item']/div[@class='all']/descendant-or-self::*[not(self::span and @class='date')]/text() 

Demo here ~ http://www.xpathtester.com/xpath/405026153e7beb1847d023f2553674b7

0
source

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


All Articles