[xsl] XSL/XPath 2.0 - most efficient way to find route to target element

Subject: [xsl] XSL/XPath 2.0 - most efficient way to find route to target element
From: Robert Koberg <rob@xxxxxxxxxx>
Date: Sat, 26 Apr 2008 11:27:07 -0400
Hi,

In the xml structure below, imagine:
- all nodes have name (not necessarily unique) and id (o;?unique)
attributes
- you are in the context of node[@id='a3'] and you want to build a path
from the name attribute to node[@id='b3'] that would result in the
shortest path, which would be:

../../../bar/baz/bat

- and in another case you are in the context of node[@id='c3'] and you
want the shortest path to node[o;?@id='b3'], which would be:

../../baz/bat

- and in another case you are in the context of node[@id='b4'] and you
want the shortest path to o;?node[o;?@id='b3'], which would be:

bat

What is the most efficient to find those paths? (this is to create page
relative links, rather than root relative, for html pages)

In the past I have just recursed up the ancestry of the calling node to
the root element and for each of those added a '../'. Then used a key to
find the target node and recurse up from that target to root prepending
the name attribute. Now, using XSL and XPath version 2, I am hoping
there is a clean, efficient way to find the shortest possible path
rather than going all the way to the root when it is not necessary.

<node name="foo" id="x">
  <node name="bar" id="b1">
    <node>
      <node/>
      <node/>
    </node>
    <node name="baz" id="b2">
      <node name="bat" id="b3"/>o;?
      <node name="bat" id="b4"/>
    </node>
    <node>o;?
    <node name="saz" id="c2">
      <node name="sat" id="c3"/>
    </node>
    </node>
  </node>
  <node name="gar" id="a1">
    <node name="gaz" id="a2">
      <node name="gat" id="a3"/>
      <node/>
    </node>
    <node>
      <node/>
    </node>
    <node>
      <node/>
    </node>
  </node>
</node>

Current Thread