This week's book giveaway is in the OCPJP forum. We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line! See this thread for details.
Unfortunately for us, you haven't posted an XPath expression. What you posted was some Java code which produces an XPath expression. It would be much easier if you posted the actual XPath expression which you say you tested. (Presumably that site just tests expressions for well-formedness.)
Joined: Jun 03, 2009
Paul Clapham wrote:Presumably that site just tests expressions for well-formedness.
Actually it highlights expression matching nodes, so I'm sure it should return a result.
I tried: //text[.="11"] and //text[.='11'].
Even when I try //text, which should return all the text nodes it gives me an empty list.
On the other hand I tried to execute equivalent code in JDom:
Here's what I think the problem is: that DTD referenced at the beginning applies a default attribute to your <svg> root element. And that attribute looks like this:
So, that declares a default namespace for the document and all of the elements are in that namespace. Your XPath expression is looking for <text> elements which are in no namespace but there aren't any. All of the <text> elements are in the "http://www.w3.org/2000/svg" namespace.
You can fix this by searching for <text> elements which are in the "http://www.w3.org/2000/svg" namespace. Attach a namespace binding to that XPath expression which has prefix "svg" and namespace URI "http://www.w3.org/2000/svg". (I don't know how to do that but there must be a way.) Then change the XPath expression to search for <svg:text> elements.
Joined: Jun 03, 2009
You're right - this was a problem of default namespace.
I found a working solution for JDOM:
But I cannot figure out how to do it with XPathAPI... I tried:
but it throws: com.sun.org.apache.xpath.internal.domapi.XPathStylesheetDOM3Exception: Prefix must resolve to a namespace: svg
Do you know what is wrong with the XPathAPI code? Or perhaps could you suggest an alternative for XPathAPI that accepts org.w3c.dom.Document.
Your document uses the default namespace. But you need a prefix for the namespace so that XPath knows to search for elements with a particular namespace URI. So my quick look at the XPathAPI documentation (that is the Xalan XPathAPI, right?) suggests you're going to have to provide a PrefixResolver to the API because of that.
And there's only one method in there which takes a PrefixResolver as a parameter, so I guess you will have to make do with that.