title: Extracting text in between nodes through XPath
body: <p>I'm trying to read specific parts of a webpage through XPath. The page is not very well-formed but I can't change that...</p>
  
  <pre><code>&lt;root&gt;
      &lt;div class="textfield"&gt;
          &lt;div class="header"&gt;First item&lt;/div&gt;
          Here is the text of the &lt;strong&gt;first&lt;/strong&gt; item.
          &lt;div class="header"&gt;Second item&lt;/div&gt;
          &lt;span&gt;Here is the text of the second item.&lt;/span&gt;
          &lt;div class="header"&gt;Third item&lt;/div&gt;
          Here is the text of the third item.
      &lt;/div&gt;
      &lt;div class="textfield"&gt;
          Footer text
      &lt;/div&gt;
  &lt;/root&gt;
  </code></pre>
  
  <p>I want to extract the text of the various items, i.e. the text in between the header divs (e.g. 'Here is the text of the first item.'). I've used this XPath expression so far:</p>
  
  <pre><code>//text()[preceding::*[@class='header' and contains(text(),'First item')] and following::*[@class='header' and contains(text(),'Second item')]]
  </code></pre>
  
  <p>However, I cannot hardcode the ending item name because in the pages I want to scrape the order of the items differ (e.g. 'First item' may be followed by 'Third item').</p>
  
  <p>Any help on how to adapt my XPath query would be greatly appreciated.</p>