Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

xpath choose all text() but not from specific tag / class attribute

Tags:

xpath

I want to ask a question:

The structure is like:

<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 choose all the text()(BY ABC DEF) but not contains <span class="date">DDD</span>(19991111)

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

Please teach me Thank you

like image 633
user2492364 Avatar asked Oct 31 '22 15:10

user2492364


1 Answers

You can combine them like this :

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

Above XPath select all text nodes within the div[@class='all'] which parent is not matched by span[@class='date'].

You may want to also filter out empty text nodes using normalize-space() :

//div[@class=item]/div[@class='all']//text()[not(parent::span[@class='date']) and normalize-space()]
like image 169
har07 Avatar answered Nov 08 '22 06:11

har07