Re: [xsl] Getting the deepest node and calculating relative paths

Subject: Re: [xsl] Getting the deepest node and calculating relative paths
From: David Carlisle <davidc@xxxxxxxxx>
Date: Wed, 24 Jan 2001 11:18:13 GMT

>  It'll be a pity if I have to "manually" perform this transformation
> using Java.

That is I am sure the more sensible solution, but lacking any sense, I
offer:


<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
                version="1.0"
                >

<xsl:output method="xml" indent="yes"/>

<xsl:variable 
 name="l" 
  select="'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ@_'"/>

<xsl:template match="Root">
<Root>
<xsl:for-each select="Paths/Path/@value">
<xsl:sort select="string-length(translate(.,$l,''))" data-type="number"/>
<xsl:if test="position()=last()">
  <LongestPath>
   <xsl:value-of select="."/>
  </LongestPath>
  <RelPaths>
  <xsl:variable name="y" select="."/>
  <xsl:variable name="x" 
        select="string-length(translate(.,$l,''))"/>
  <xsl:for-each select="/Root/Paths/Path">
  <RelPath>
  <xsl:attribute name="value">
  <xsl:choose>
  <xsl:when test="@value=$y">.</xsl:when>
  <xsl:otherwise>
  <xsl:call-template name="r">
  <xsl:with-param name="x" select="$x -
       string-length(translate(@value,$l,''))"/>
  </xsl:call-template>
  <xsl:call-template name="l"/>
  </xsl:otherwise>
  </xsl:choose>
  </xsl:attribute>
  </RelPath>
  </xsl:for-each>
  </RelPaths>
</xsl:if>
</xsl:for-each>
</Root>
</xsl:template>

<xsl:template name="r">
<xsl:param name="x" />
<xsl:if test="$x &gt;= 0">../<xsl:call-template
 name="r">
  <xsl:with-param name="x" select="$x -1"/>
</xsl:call-template>
</xsl:if>
</xsl:template>

<xsl:template name="l">
<xsl:param name="s" select="@value"/>
<xsl:choose>
<xsl:when test="contains($s,'/')">
<xsl:call-template name="l">
 <xsl:with-param name="s" select="substring-after($s,'/')"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
 <xsl:value-of select="$s"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>

</xsl:stylesheet>




bash-2.01$ xt paths.xml paths.xsl
<?xml version="1.0" encoding="utf-8"?>
<Root>
<LongestPath>/Catalog/Department/Product/Variant/Name</LongestPath>
<RelPaths>
<RelPath value="../../SKU"/>
<RelPath value="../../../@dept_id"/>
<RelPath value="../../Name"/>
<RelPath value="../../Price"/>
<RelPath value="."/>
</RelPaths>
</Root>
bash-2.01$ 


 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread