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

时间:2017-09-11 04:56:22

标签: 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>标记文字,但我希望它在stopHeader类之前的最后<p></p>停止。

这是我提出的代码:

//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")])]

2 个答案:

答案 0 :(得分:2)

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

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

这应该返回h2"Preparation"之间每个标题("Execution")的文字内容

答案 1 :(得分:0)

试试这个xpath。

//h2[text()='Preparation']/following::h2[not(@class='stopHeader')]/text()