Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

XPathAPI with SVGDocument

 
Krzysztof Jurkowski
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I want to create a find tool for my JSVGCanvas, so that user can zoom on a certain text in the drawing.
The svg file looks as follows:



To do it I just need to select a text node in the SVGDocument, get the x and y attributes and I have the coordinates or the node on the drawing.

The problem is that when I call:

than the returned list is empty.

Same story with:

it returns null!

I tested this XPath expression with http://ponderer.org/download/xpath/ and it works fine.
What am I doing wrong?
 
Paul Clapham
Sheriff
Posts: 21107
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.)
 
Krzysztof Jurkowski
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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'].
None works.

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:

and it also returns an empty list.
So something must be wrong with the svg document or the XPath expression.
If you wish you can download a sample document from:
http://dl.getdropbox.com/u/296382/tree.svg
 
Paul Clapham
Sheriff
Posts: 21107
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Krzysztof Jurkowski
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Thanks!
 
Paul Clapham
Sheriff
Posts: 21107
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic