percentum
percentum

Reputation: 153

XPATH: p element who's immediate preceding sibling is a p

I am trying to select the first p element who's direct preceding sibling is also a p element. I have highlighted the below code with example of what i am looking for please.

<div class="vacancy-left">
    <h2>Machine Operator / Print Operator</h2>
    <h3>Vacancy Ref:</h3>
    <p>I-1056</p>   
    <p>Are you self-motivated with a..</p> <!-- THIS NODE -->           
    <p>The ideal candid...</p>
    <p>Hours of Work Monday- Friday 07.30-16.30</p>             
    <h3>Salary:</h3>
    <p>Start pay from £8.00 per hour</p>
    <p> Immediate Start</p>
    <h2><a href="../../contact.php">Apply For This Vacancy</a></h2>
    <p></p>
    <p><a href="../industrial.htm">Return to Industrial Vacancies</a></p>
</div>

I've tried a couple things such as .//p[preceding-sibling::p] but that selects a few p elements that have any preceding element as a p. Thanks

Upvotes: 0

Views: 404

Answers (1)

Martin Honnen
Martin Honnen

Reputation: 167516

You can use //p[preceding-sibling::*[1][self::p]] to select all p elements where the nearest preceding sibling element is also a p element, however it would select more than one p in your sample as several p elements have another p element as the nearest preceding sibling element.

Upvotes: 1

Related Questions