dom - 使用 XPath 到达特定类时如何停止查询?

标签 dom xpath

假设我有以下内容:

<div class="data">
    <h2 class="entry-contentH2">Preparation</h2>
    <h2>Airplanes</h2>
    <ul>
        <li><strong>3 large</strong> wings</li>
        <li><strong>2</strong>doors</li>
    </ul>
    <h2>Car</h2>
    <ul>
        <li><strong>4</strong> doors</li>
        <li><strong>1 cup</strong> holder</li>
    </ul>
    <h2 class="stopHeader">Execution</h2>
    <h2>Motorcycles</h2>
    <ul>
        <li>Easy to learn</li>
    </ul>
</div>

我正在尝试查询所有 <p></p> <h2>Preparing</h2> 之后的标签文本, 但我希望它停在最后 <p></p>在 stopHeader 类之前。

这是我想出的代码:

//h2[contains(.,"Preparation")]/following-sibling::h2/text()[not(preceding::h2[@class="stopHeader"])]

#and also
//h2[contains(.,"Preparation")]/following-sibling::h2/text()[not(preceding::h2[contains(., "Execution")])]

最佳答案

尝试以下 XPath 以获得所需的输出:

//h2[.="Preparation"]/following-sibling::h2[./following-sibling::h2[.="Execution"]]/text()

这应该返回 "Preparation""Execution"

之间每个标题 (h2) 的文本内容

关于dom - 使用 XPath 到达特定类时如何停止查询?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46148407/

相关文章:

ruby - 删除标签但保留文本

XPath/XQuery : find text in a node, 但忽略特定后代元素的内容

javascript - 如何禁用带有定时循环的按钮?

Javascript 比另一个属性上的 XSLT generate-id() 更好地识别回调节点

jquery - 使用 jQuery 遍历元素属性

c# - XPath 和属性

xpath - 如何在 HTML <br> 标记之间拆分 HTML/XML 文本?

javascript - DOM 节点生成 HTML5 可能吗?

java - 实现 DOM 意味着什么

python - 在 xpath 中使用 "or"运算符时合并输出