假设我有一些如下所示的 html:
<section>
<header>
<h1>Foo</h1>
</header>
<section>
<a href="#">one</a>
</section>
<section>
<a href="#">two</a>
</section>
</section>
<section>
<header>
<h1>Bar</h1>
</header>
<section>
<a href="#">one</a>
</section>
<section>
<a href="#">two</a>
</section>
</section>
我想获取文本为“two”的 anchor ,但必须属于标题为“Bar”的部分标记(即上述 html 中的最后一个 anchor )。
使用 Bar 文本获取 header 元素很容易:
//header/h1[text()='Bar']
也可以轻松获取位于节元素中的文本“two”的两个 anchor :
//section/a[text()='two']
这当然会返回两个 anchor 。
我只是不确定如何将两者结合起来,以便我只能在包含“Bar”的标题元素的同级元素中找到 anchor 。
最佳答案
//section[header/h1 = 'Bar']/section/a[. = 'two']
关于xml - 作为另一个节点的同级节点的节点内的节点的 xpath,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7266657/