From: Diego Date: 2008-11-03T13:48:40+09:00 Subject: Using XPath: find last text node of each paragraph under the root node. I want to trim trailing whitespace at the end of all XHTML paragraphs. I am using the REXML library. Say I have the following in a valid XHTML file:

hello world a

Hi there

The End

I want to end up with this:

hello world a

Hi there

The End

So I was thinking what I could use XPath to get just the text nodes that I want, then just trim the text, which would allow me to end up with what I want (previous). I started with the following XPath: //root/p/child::text() Of course, the problem here is that it returns all text nodes that are children of all p-tags. Which is this: 'hello ' ' a ' 'Hi there ' 'The End ' 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. //root/p/child::text()[last()] This only returns: 'The End ' What I would like to get from the XPath is therefore: ' a ' 'Hi there ' 'The End ' I have tried //root//p/child::text()[last()] on other XPath parsers, and it works. Could just be a bug (or different interpretation of the rules) by REXML? Cheers, Diego