Is there a Java API which can generate an XPath String from the source node to the destination node?

Brian Ewins

Your best bet is to write a class that extends org.xml.sax.helpers.XMLFilterImpl. If you use a SAX parser on your documents you can insert this in your app to trap startElement and endElement events, and use these to maintain a stack of elements. You would then only need a method to empty the stack (setting your source node to be the current one) and another method to read the stack as a string (ie reading the relative position of the destination node). endElements recieved which don't have a matching startElement in the stack would generate a '../' XPath; you also need to keep track of closed tag pairs at any element (in order to figure out 'position()'). None of this is very hard; even calculating the reverse path isn't too bad. I believe there is an IBM tool at http://alphaworks.ibm.com/ which can do the calculations for you - in an editor - and similarly there's an emacs addon for psgml-mode at http://www.megginson.com/ . However I've not seen a publicly available API which will do the task described.