xml - Xpath:第一个前后兄弟

标签 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>

我错过了什么?

我将如何更改 xpath 以包含 li/[a],就像我包含 p/[a] 一样? preceding/following-sibling 不适用于 li

最佳答案

你应该只需要指定它是第一个 preceding-sibling h2:

preceding-sibling::h2[1]

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

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

此外,如果您需要确定第一个不是 p 的 following-sibling 是 hr,您可以试试这个...

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

如果除了 p 之外,您还尝试选择 li,您可以更新 xpath 以使用 preceding: :following::,但您必须考虑可能作为 p 的子元素出现的任何元素,例如 a , span, 等等...

//*[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]]

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

<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>

但是,我建议使用第二个 xpath 专门针对 li...

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

关于xml - Xpath:第一个前后兄弟,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50608006/

相关文章:

ruby - Mechanize /Nokogiri 无法使用 xpath 解析 XML

xml - 根据 XSLT 中的子节点值选择节点

java - Xpath div 在每次新构建后都会更改

java - JBoss7 + PostgreSQL 新的缺失/未满足的依赖关系

python - 使用 python 处理 selenium 中的应用程序下载窗口

java - 为什么当我单击按钮时我的页面不存在?

xml - 错误 : schemaLocation does not contain namespace-location pairs

php - 使用 Xpath 获取第一个祖先

c# - 如何从元素中具有相同名称的 xml 文件中获取特定值?

javascript - 是否有 Node.js 的 XSD 验证器