This post originated from an RSS feed registered with Java Buzz
by Chris Winters.
Original Post: Useful but probably common XPath function
Feed Title: cwinters.com
Feed URL: http://www.cwinters.com/search/registrar.php?domain=jroller.com®istrar=sedopark
Feed Description: Chris Winters on Java, programming and technology, usually in that order.
I don't work with XML often enough to have this always at the ready, so: normalize-space() will take care of chomping sequences of whitespace to a single space. So when you're trying to match something like:
<p>This is my
Great Page Number Three
summary
</p>
You can just use something like this and it will do the right thing (using dom4j if that matters):
String path = "//p[contains( normalize-space( text() ), 'This is my Great Page Number' )]";
List matching = doc.selectNodes( path );
(...and when I say 'XPath function' I actually mean 'XSLT function also used by XPath'. But since I'm just using XPath right now...)