Sourabh Karmarkar
Sourabh Karmarkar

Reputation: 87

Matching all the elements and text excluding one by using XPath

<div class="main">
  <div class="column-items">
    <div>
      abcd
    </div>
    <h2>Thomas</h2>
    <p>Ron</p>
  </div>
  <p>Dave</p>
  Sample text here......
  <b>Andrew</b>
  El murciélago de Bacardí tiene su!!
  <p>abcdef</p>
</div>

In the above structure I want the XPath to get all the content including text inside the <div class="main"> except that I don't want the content of the first div element i.e <div class="column-items">.

Upvotes: 1

Views: 725

Answers (2)

kjhughes
kjhughes

Reputation: 111491

The question title asks for all the elements and text excluding one, which you go on to say that the first element should be excluded. If you actually only want text nodes that meet that criteria, see @JaSON's answer.

If you do indeed want all the elements too, use this XPath:

//div[@class='main']/node()[not(self::div[1])]

based on position, or this XPath,

//div[@class='main']/node()[not(self::div[@class="column-items"])]

based upon attribute value.

Upvotes: 1

JaSON
JaSON

Reputation: 4869

If you want all text nodes from 'main' div excluding text nodes from 'column-items' div, try:

//div[@class='main']//text()[not(ancestor::div[@class="column-items"])]

Upvotes: 2

Related Questions