Stark
Stark

Reputation: 593

How to stop querying when it reaches a specific class with XPath?

Say I have the following:

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

I'm trying to get query all of the <p></p> tags text after the <h2>Preparing</h2>, but I want it to stop at the last <p></p> before the stopHeader class.

This is the code that I came up with:

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

Upvotes: 3

Views: 110

Answers (2)

Jainish Kapadia
Jainish Kapadia

Reputation: 2611

Try this xpath.

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

Upvotes: 0

Andersson
Andersson

Reputation: 52685

Try below XPath to get desired output:

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

This should return text content of each header (h2) between "Preparation" and "Execution"

Upvotes: 2

Related Questions