title: Using XPath: find last text node of each paragraph under the root node
body: <p>I want to trim trailing whitespace at the end of all XHTML paragraphs. I am using Ruby with the REXML library.</p>
  
  <p>Say I have the following in a valid XHTML file:</p>
  
  <pre><code>&lt;p&gt;hello &lt;span&gt;world&lt;/span&gt; a &lt;/p&gt;
  &lt;p&gt;Hi there &lt;/p&gt;
  &lt;p&gt;The End &lt;/p&gt;
  </code></pre>
  
  <p>I want to end up with this:</p>
  
  <pre><code>&lt;p&gt;hello &lt;span&gt;world&lt;/span&gt; a&lt;/p&gt;
  &lt;p&gt;Hi there&lt;/p&gt;
  &lt;p&gt;The End&lt;/p&gt;
  </code></pre>
  
  <p>So I was thinking I could use XPath to get just the text nodes that I want, then trim the text, which would allow me to end up with what I want (previous).</p>
  
  <p>I started with the following XPath:</p>
  
  <pre><code>//root/p/child::text()
  </code></pre>
  
  <p>Of course, the problem here is that it returns all text nodes that are children of all p-tags. Which is this:</p>
  
  <pre><code>'hello '
  ' a '
  'Hi there '
  'The End '
  </code></pre>
  
  <p>Trying the following XPath gives me the last text node of the last paragraph, not the last text node of each paragraph that is a child of the root node.</p>
  
  <pre><code>//root/p/child::text()[last()]
  </code></pre>
  
  <p>This only returns: <code>'The End '</code></p>
  
  <p>What I would like to get from the XPath is therefore:</p>
  
  <pre><code>' a '
  'Hi there '
  'The End '
  </code></pre>
  
  <p>Can I do this with XPath? Or should I maybe be looking at using regular expressions (That's probably more of a headache than XPath)?</p>