xpath:选择没有给定元素的所有兄弟姐妹?

时间:2010-11-15 09:51:32

标签: html xpath

<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])]选择text3标题。

<i>foo</i>可以处于任何位置!它可能并不总是处于上述位置。

我想选择没有h1的{​​{1}}元素,所以我应该

foo

2 个答案:

答案 0 :(得分:1)

使用此XPath查询:

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

答案 1 :(得分:0)

关于我的评论,你可以做的是:

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

甚至

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

更新:如果您认为前面的h1是“包含”foo,那么您可以这样做:

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

这适用于例如。

<h1>text1</h1> <b> description1 </b> 
<h1>text2</h1> <b> description2 </b> <i>foo</i>
<h1>text3</h1> 
<h1>text4</h1> <b> description4 </b> 
相关问题