KJW
KJW

Reputation: 15251

xpath: selecting all siblings without given element?

<html> 
<body> 
<div id="products"> 
<h1>text1</h1> <b> description1 </b> <i>foo</i>
<h1>text2</h1> <b> description2 </b> 
<h1>text3</h1> 
<h1>text4</h1> <b> description4 </b> 
</div> 
</body> 
</html>

//h1[not(following-sibling::*[1][self::b])] selects the text3 heading.

<i>foo</i> can be in any position ! It may not always be in the position described above.

I want to select the h1 element which doesn't have foo, so I should get

text2,text3,text4

Upvotes: 1

Views: 3500

Answers (2)

Use this XPath query:

//h1[following-sibling::h1/following-sibling::i] | //h1[preceding-sibling::i]

Upvotes: 1

Felix Kling
Felix Kling

Reputation: 816242

Regarding my comment, what you could do is this:

//i[contains(.,'foo')]/following-sibling::h1

or even

//*[contains(.,'foo')]/following-sibling::h1

Update: If you consider the preceding h1 as the one that "contains" foo, you could do this:

//*[contains(., 'foo')]/(following-sibling::h1 | remove(preceding-sibling::h1, 1)))

this works for e.g.

<h1>text1</h1> <b> description1 </b> 
<h1>text2</h1> <b> description2 </b> <i>foo</i>
<h1>text3</h1> 
<h1>text4</h1> <b> description4 </b> 

Upvotes: 0

Related Questions