You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@xalan.apache.org by "CHAUDHURI, Neil" <NC...@lmi.org> on 2003/12/15 20:33:43 UTC

Retrieving XPath Expressions for a Particular Node

For the first of what will likely be many messages on here, I am hoping to locate certain elements in a an XML Schema document that are significant to my application.  The issue is that while errors are reported with line and column numbers, I do not believe there is an equivalent capability for identifying locations in the document of correct data.  I therefore figured XPath was the best option.  Since it appears that Xerces (whose XSModel abstraction I have been using for the schema processing) has no XPath capabilities, I decided to look at Xalan which should, as a transformation engine, have XPath capabilities.  It seems though that the XPathAPI class is only capable of locating a node given a context node and an XPath expression to be evaluated from the context.  What I am looking for is similar but with the XPath input to be "exchanged" with the node output.

My question is this: Is there any way that I can retrieve the XPath expression for a node given the node itself and some context node from which the expression must be evaluated to locate the node in question.  Or put perhaps more simply, if I provide a context node A and some other node B, can I retrieve the XPath expression that takes me from A to B?

I appreciate any insights.  Thanks very much. 

Neil
____________________________
Neil Chaudhuri
Research Fellow
Systems Development & Technology Support
Logistics Management Institute
(571) 633-7786
nchaudhuri@lmi.org
www.lmi.org


RE: Retrieving XPath Expressions for a Particular Node

Posted by Rick Bullotta <ri...@lighthammer.com>.
Interesting stuff, Joe.  We've taken a similar approach, with a GUI
front-end for selecting a node, but "behind the scenes" we munge up the
namespaces and prefixes with "pseudo-prefixes" <g>.  Basically, for each
namespace (including the ever-unpopular prefix-less default namespace), we
create a dummy prefix declaration of ns1, ns2, ns3, etc...and when the user
selects a node in the GUI, the appropriate xpath is generated by walking the
DOM from the root and applying the "universal" NS1, NS2, NS3 and any
positional indexes, using "peer level siblings" with the same name.  We're
now adding other ways to contextualize a node through the GUI, such as value
matching on peer or parent nodes/attributes.

Of course, this approach does a simple transversal of the DOM to build up
the initial "fake namespaces", so a major structural change to the document
would invalidate any subsequent attempts to use the generated xpath.  But
for our uses, which are mostly accessing SAP XML documents and web service
output, it works spectacularly well, and really makes the user's life
pleasant compared to the sheer hell of dealing the specific namespaces and
manually entering complex xpath expressions.

Rick Bullotta
CTO
Lighthammer Software (http://www.lighthammer.com)

-----Original Message-----
From: Joseph Kesselman [mailto:keshlam@us.ibm.com] 
Sent: Monday, December 15, 2003 3:41 PM
To: xalan-dev@xml.apache.org
Cc: xalan-dev@xml.apache.org
Subject: Re: Retrieving XPath Expressions for a Particular Node





Generating XPaths to a specific node is an outstanding wishlist item. It
wouldn't be hard... except that XPath failed to define any way of declaring
namespaces, so we'd need to invent some magic to go along with it to create
those declarations (and, if necessary, new prefixes to disambiguate cases
where a prefix was reused).

I discussed some of those issues, and showed a non-namespace-correct
"pseudo XPath" generator template, in my article on styling stylesheets:
      http://www-106.ibm.com/developerworks/xml/library/x-styless2/
That logic could be trivially rewritten in Java or C++, but namespaces
would remain a nuisance.

The paths it produces are from the root rather than from a given source
node, which may not be optimal for your needs. It could probably be
rewritten to find the lowest common ancestor and go through that instead,
but even that may not be as good a choice as an XPath constructed manually.

Have fun. If you come up with anything really good, consider sharing it
with us.

______________________________________
Joe Kesselman, IBM Next-Generation Web Technologies: XML, XSL and more.
"The world changed profoundly and unpredictably the day Tim Berners Lee
got bitten by a radioactive spider." -- Rafe Culpin, in r.m.filk



Re: Retrieving XPath Expressions for a Particular Node

Posted by Joseph Kesselman <ke...@us.ibm.com>.



Generating XPaths to a specific node is an outstanding wishlist item. It
wouldn't be hard... except that XPath failed to define any way of declaring
namespaces, so we'd need to invent some magic to go along with it to create
those declarations (and, if necessary, new prefixes to disambiguate cases
where a prefix was reused).

I discussed some of those issues, and showed a non-namespace-correct
"pseudo XPath" generator template, in my article on styling stylesheets:
      http://www-106.ibm.com/developerworks/xml/library/x-styless2/
That logic could be trivially rewritten in Java or C++, but namespaces
would remain a nuisance.

The paths it produces are from the root rather than from a given source
node, which may not be optimal for your needs. It could probably be
rewritten to find the lowest common ancestor and go through that instead,
but even that may not be as good a choice as an XPath constructed manually.

Have fun. If you come up with anything really good, consider sharing it
with us.

______________________________________
Joe Kesselman, IBM Next-Generation Web Technologies: XML, XSL and more.
"The world changed profoundly and unpredictably the day Tim Berners Lee
got bitten by a radioactive spider." -- Rafe Culpin, in r.m.filk