getting XPath of a node

J

Josef Garvi

Is it possible with the new 1.5 JDK's XPath libraries to retrieve the XPath
of a given node?

Something like:

String xpathExpression = XPathFactory.createFromNode(doc, node);


--
Josef Garvi

"Reversing desertification through drought tolerant trees"
http://www.eden-foundation.org/

new income - better environment - more food - less poverty
 
C

Chris Smith

Josef said:
Is it possible with the new 1.5 JDK's XPath libraries to retrieve the XPath
of a given node?

Something like:

String xpathExpression = XPathFactory.createFromNode(doc, node);

I don't see anything off-hand.

In general, there are an infinite number of different valid XPath
expressions that will refer to the same node. A lot of choices would
need to be made (for example, whether to index into child nodes by some
attribute value, or positionally -- and if positionally, should only
elements of a given tag name be considered or should the global position
be used, etc.) Once you've resolved all of these ambiguities, it
shouldn't be too difficult to implement this from a DOM model.

--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
B

Bryce (Work)

I don't see anything off-hand.

I would think the default way would be from the root, such as
/rootNode/someParent/anotherParent/ourNode

XML Spy can do it. I don't know much about the new XPath libraries,
but if you wanted to write it your self, I guess you'd just start with
your node, traverse to it's parent, etc. Each node will only have one
parent. Keep traversing til you get to the root. Has the markings of a
recursive function to me... Something like:

Get Parent Node:
if Has Parent
Get Parent Node

return name
 
C

Chris Smith

Bryce said:
I would think the default way would be from the root, such as
/rootNode/someParent/anotherParent/ourNode

XML Spy can do it. I don't know much about the new XPath libraries,
but if you wanted to write it your self, I guess you'd just start with
your node, traverse to it's parent, etc. Each node will only have one
parent. Keep traversing til you get to the root.

That would certainly be doable if you can assume, for example, that
there is only one child of "rootNode" called "someParent". If not, then
the ambiguities I mentioned begin to take place.

Incidentally, it would be just as easy to implement this iteratively as
recursively:

String path = "";
for (Node n = node; n != doc; n = n.getParentNode())
{
String localXPath = ...;
path = localXPath + "/" + path;
}

path = "/" + path;

Since the loop is unlikely to be long-lived, I wouldn't worry about the
string building inefficiencies there.

--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
J

Josef Garvi

Chris said:
That would certainly be doable if you can assume, for example, that
there is only one child of "rootNode" called "someParent". If not, then
the ambiguities I mentioned begin to take place.

I noticed that XPath Explorer (http://sourceforge.net/projects/xpe/) solves
this by positional indexing within the element type. It gives a unique
reference to the child node as long as the order of child nodes remains
unchanged. Modifying the document (without modifying the node in question)
will invalidate the obtained path.

Referencing to child nodes by some unique attribute would be better in that
sense, but it's hard to do generically. I guess it could be done by
analysing the schema file in some way, though... (assuming that all
elements have some kind of unique identification).

--
Josef Garvi

"Reversing desertification through drought tolerant trees"
http://www.eden-foundation.org/

new income - better environment - more food - less poverty
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top