File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Other JSE/JEE APIs and the fly likes XPathAPI with SVGDocument Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Other JSE/JEE APIs
Bookmark "XPathAPI with SVGDocument" Watch "XPathAPI with SVGDocument" New topic
Author

XPathAPI with SVGDocument

Krzysztof Jurkowski
Greenhorn

Joined: Jun 03, 2009
Posts: 8
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
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

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

Joined: Jun 03, 2009
Posts: 8
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
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

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

Joined: Jun 03, 2009
Posts: 8
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
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: XPathAPI with SVGDocument
 
Similar Threads
java centre point (x,y) of line
A stack to be accessed by various pages
Help me how to drag and move drawing shaps
How to code optimization..
Moving Shapes between two Points