I'm using Java 6. Let's say I have a document with these nodes ...
I want to write an Xpath expression to select all the nodes (they may not necessarily be "p"'s) between the <h2> nodes with text descriptions "Description" and "Cost", as appear above. How do I do this?
but get the Java exception, "com.sun.org.apache.xpath.internal.XPathException: Can not convert #BOOLEAN to a NodeList!". - Dave
Aha. You're missing the namespace. Your "h2" in the XPath expression refers to an h2 element which is in no namespace, but the h2 elements in the document you linked to are actually in the XHTML namespace.
So provide a namespace context for your XPath object -- let's say it maps the "http://www.w3.org/1999/xhtml" namespace URI to the prefix "x" -- and then use "x:h2" instead of "h2" for the element names.
Joined: Jul 02, 2008
Thanks, but I don't think this is a namespace issue. For example, this expression accurately returns an element ...
Notice the use of "h2" without a preceding namespace. The above returns the "<h2>Description</h2>" element from the page. Here is the Java code I'm using to find the element ...