Xpath:第一个前后兄弟

时间:2018-05-30 15:05:04

标签: xml xpath

XML:

<body>
    <h2><font style="font-weight: bold">Baz</font></h2>
    <p><img title="image" /></p>
    <p>Baz 0 with an <a href="http://">anchor</a> element.</p>
    <p>Baz 1 with an <a href="http://">anchor</a> element.</p>
    <hr />
    <h2><font style="font-weight: bold">People</font></h2>
    <ul>
        <li>People 0 with <a href="http://" >an anchor</a> element.</li>
        <li>People 1 with an <a href="http://" >an anchor</a> element.</li>
    </ul>
    <hr/>
    <h2><font style="font-weight: bold">Sales</font></h2>
    <ul>
        <li>List item 2 with an <a href="http://" >an anchor</a> element.</li>
        <li>List item 3 with an <a href="http://" >an anchor</a> element.</li>
        <li>List item 4 without an anchor element.</li>
    </ul>
    <hr />
    <h2><font style="font-weight: bold">Sales</font></h2>
    <p><img title="image" /></p>
    <p>sales 0 with an <a href="http://">anchor</a> element.</p>
    <p>sales 1 with an <a href="http://">anchor</a> element.</p>
    <hr />
    <h2><font style="font-weight: bold">Foo</font></h2>
    <ul>
        <li>Foo 0 with <a href="http://" >an anchor</a> element.</li>
        <li>Foo 1 with an <a href="http://" >an anchor</a> element.</li>
    </ul>
    <hr />
    <h2><font style="font-weight: bold">Bar</font></h2>
    <p><img title="image" /></p>
    <p>bar 0 with an <a href="http://">anchor</a> element.</p>
    <p>bar 1 with an <a href="http://">anchor</a> element.</p>
    <hr />
</body>

此xpath://p[a and preceding-sibling::h2[font[text()='Sales']][1] and following-sibling::hr[1]]

返回:

<p>sales 0 with an <a href="http://">anchor</a> element.</p>
<p>sales 1 with an <a href="http://">anchor</a> element.</p>
<p>bar 0 with an <a href="http://">anchor</a> element.</p>
<p>bar 1 with an <a href="http://">anchor</a> element.</p>

所需p

<p>sales 0 with an <a href="http://">anchor</a> element.</p>
<p>sales 1 with an <a href="http://">anchor</a> element.</p>

所需li

<li>List item 2 with an <a href="http://" >an anchor</a> element.</li>
<li>List item 3 with an <a href="http://" >an anchor</a> element.</li>

我错过了什么?

如何以与li/[a]相同的方式更改xpath以包含p/[a]preceding/following-sibling不适用于li

1 个答案:

答案 0 :(得分:2)

你应该只需要指明它是第一个兄弟姐妹h2:

preceding-sibling::h2[1]

更新了xpath(我还简化了Sales的测试):

//p[a and preceding-sibling::h2[1][.='Sales'] and following-sibling::hr]

此外,如果你需要确定第一个不是p的后续兄弟是hr,你可以试试这个......

//p[a and preceding-sibling::h2[1][.='Sales'] and following-sibling::*[not(self::p)][1][self::hr]]

除了li之外,如果您还尝试选择p可以更新xpath以使用preceding::和{{1但是,您必须考虑可能显示为following::的子项的任何元素,如pa等...

span

这将从您的示例XML中选择以下内容......

//*[self::p or self::li][a and preceding::h2[1][.='Sales'] and following::*[not(self::p) and not(self::li) and not(self::a)][1][self::hr]]

但是,我建议第二个xpath专门定位<li>List item 2 with an <a href="http://" >an anchor</a> element.</li> <li>List item 3 with an <a href="http://" >an anchor</a> element.</li> <p>sales 0 with an <a href="http://">anchor</a> element.</p> <p>sales 1 with an <a href="http://">anchor</a> element.</p> ...

li