Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

dom

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")])]
like image 568
Stark Avatar asked Jan 29 '26 06:01

Stark


1 Answers

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"

like image 89
Andersson Avatar answered Jan 31 '26 22:01

Andersson