节点内节点的xpath,是另一个节点的兄弟节点

时间:2011-09-01 06:12:09

标签: xml xpath

假设我有一些看起来像这样的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'文本的锚点,但必须属于带有'Bar'标题的section标签(即上面html中的最后一个锚点)。

使用Bar文本获取标题元素很容易:

//header/h1[text()='Bar']

使用文本“two”的两个锚点也很容易生成一个section元素:

//section/a[text()='two']

当然会返回两个锚点。

我只是不确定如何将两者结合起来,这样我才会发现锚点位于section元素中,该元素是包含'Bar'的header元素的兄弟。

1 个答案:

答案 0 :(得分:3)

//section[header/h1 = 'Bar']/section/a[. = 'two']
相关问题