Majid Fouladpour
Majid Fouladpour

Reputation: 30252

PHP - Returning all paragraphs up to first <h2>

Wikipedia articles have this structure:

<div id="bodyContent"> 
  <div id="siteSub">...</div> 
  <div id="contentSub"></div> 
  <div id="jump-to-nav">...</div> 
  <table class="infobox vevent">...</table> 
  <p>Article summary</p>
  <p>Article summary continued</p>
  <p>Article summary continued</p>
  <table id="toc" class="toc">...</table> 

  <h2>...</h2> 
  <p>...</p>
  <p>...</p>
</div>

I am interested in the summary part. With Xpath, I want to say:

Return <p> nodes inside #bodyContent from the start AND stop as soon as you encounter the first <h2>

How do I say this?

Upvotes: 1

Views: 205

Answers (1)

lonesomeday
lonesomeday

Reputation: 237965

I think you want something like //div[@id="bodyContent"]/h2[1]/preceding-sibling::p.

This says "from #bodyContent's children, find the first h2 element and among its preceding siblings find all p elements".

Upvotes: 3

Related Questions