xsl-list
[Top] [All Lists]

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

2008-04-26 15:43:36


This doesn't get quite the result you ask for I get ../bat for the last
one, also I assume that nodes without a name should not contribute to
the path (otherwise I'd need an extra ../ to get out of the unnamed
node that contains c2).

<xsl:stylesheet version="2.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
                xmlns:f="data:,f">
 
<xsl:key name="id" match="*" use="@id"/>
<xsl:variable name="r" select="/"/>
<xsl:template match="/">

a3 to b3 <xsl:value-of select="f:path('a3','b3')"/>
c3 to b3 <xsl:value-of select="f:path('c3','b3')"/>
b4 to b3 <xsl:value-of select="f:path('b4','b3')"/>

</xsl:template>


<xsl:function name="f:path">
 <xsl:param name="a"/>
 <xsl:param name="b"/>
 <xsl:variable name="an" select="key('id',$a,$r)"/>
 <xsl:variable name="bn" select="key('id',$b,$r)"/>
 <xsl:value-of>
  <xsl:sequence select="($an/ancestor-or-self::node[(_at_)name] except 
$bn/ancestor::node)/'../'"/>
  <xsl:value-of separator="/" select="($bn/ancestor-or-self::node[(_at_)name] 
except $an/ancestor::node)/@name"/>
 </xsl:value-of>
</xsl:function>

</xsl:stylesheet>

________________________________________________________________________
The Numerical Algorithms Group Ltd is a company registered in England
and Wales with company number 1249803. The registered office is:
Wilkinson House, Jordan Hill Road, Oxford OX2 8DR, United Kingdom.

This e-mail has been scanned for all viruses by Star. The service is
powered by MessageLabs. 
________________________________________________________________________

--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--