<div>
<div class="header">
<p>name 1</p>
</div>
<div class="content">czx</div>
<div class="content">dsczx</div>
<div class="content">czsadx</div>
<div class="content">cz34x</div>
<div class="content">4czewtwex</div>
<div class="header">
<p>name 2</p>
</div>
<div class="content">czx</div>
<div class="content">czsadx</div>
<div class="content">cz34x</div>
<div class="content">4czewtwex</div>
<div class="header">
<p>name 3</p>
</div>
<div class="content">czx</div>
<div class="content">czsadx</div>
<div class="content">cz34x</div>
<div class="content">4czewtwex</div>
</div>
嗨大家好!
我有一个像这样的问题结构。我想创建一个xpath,它将获得给定<div>
下面的所有<p>
元素,这些元素具有类'content',但是如果有任何其他元素具有类'header',则'header'下面的元素将被省略。
//div/p[text() = 'name 1']/../following-sibling::div[@class = 'content']
输出//div/p[text() = 'name 1']/../following-sibling::div[@class = 'content']
的应为:
<div class="content">czx</div>
<div class="content">dsczx</div>
<div class="content">czsadx</div>
<div class="content">cz34x</div>
<div class="content">4czewtwex</div>
输出//div/p[text() = 'name 2']/../following-sibling::div[@class = 'content']
的应为:
<div class="content">czx</div>
<div class="content">czsadx</div>
<div class="content">cz34x</div>
<div class="content">4czewtwex</div>
输出//div/p[text() = 'name 3']/../following-sibling::div[@class = 'content']
的应为:
<div class="content">czx</div>
<div class="content">czsadx</div>
<div class="content">cz34x</div>
<div class="content">4czewtwex</div>
答案 0 :(得分:0)
它有点复杂(我确信它可以做得更好)。注意&#39;名称X&#39;用于3个地方(你需要更换它3次)。
/ div / div [preceding-sibling :: div / p =&#39; name 3&#39;]
count(/ div / div [preceding-sibling :: div / p =&#39; name 3&#39;])
计数(/ DIV / DIV [前同辈:: DIV / P =&#39;名称 3&#39;] [@类=&#39;头&#39;] /以下:: DIV)
我们的子列表总数减去我们要删除的元素数量 来自清单。
xPath(wokring solution):
/div/div[preceding-sibling::div/p='name 3'][position()<count(/div/div[preceding-sibling::div/p='name 3'])+1-count(/div/div[preceding-sibling::div/p='name 3'][@class='header']/following::div)][@class='content']