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:[EMAIL PROTECTED] 
Sent: Monday, December 15, 2003 3:41 PM
To: [EMAIL PROTECTED]
Cc: [EMAIL PROTECTED]
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


Reply via email to