Markus
Markus

Reputation: 4242

Xpath Expression - Select between two known Tags

Given the following HTML:

<body>
    <h2>
        <span class="mw-headline" id="KNOWNID">S</span>
        <span class="mw-editsection">
            <span class="mw-editsection-bracket">[</span>
            <a href="/w/index.php?title=Og&amp;action=edit&amp;section=4">Bn</a>
            <span class="mw-editsection-bracket">]</span>
        </span>
    </h2>
    <div class="h"></div>
    <p>M</p>
    <p>M</p>
    <p>M</p>
    <p>M</p>
    <p>M</p>
    <h2>
        <span class="mw-headline" id="NEXTKNOWNID">S</span>
        <span class="mw-editsection">
            <span class="mw-editsection-bracket">[</span>
            <a href="/w/index.php?title=Og&amp;action=edit&amp;section=4">Bn</a>
            <span class="mw-editsection-bracket">]</span>
        </span>
    </h2>
</body>

I have the ID="KNOWNID" and need to select all <p></p> Elements until the id="NEXTKNOWNID". How can this be acomplised with XPATH?

Thanks

Upvotes: 1

Views: 614

Answers (1)

alecxe
alecxe

Reputation: 473753

Use the preceding-sibling and following-sibling:

//p[preceding-sibling::h2/span[@id="KNOWNID"] and following-sibling::h2/span[@id="NEXTKNOWNID"]]

Upvotes: 3

Related Questions