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

Subject: RE: [xsl] Getting the deepest node and calculating relative paths
From: Iresh Mehta <imehta@xxxxxxxxxxxxxxxxxxx>
Date: Thu, 25 Jan 2001 11:10:33 +0530
That was awesome David, thanks! Now I just need to digest it - it's a bit
too much for a newbie like me. A small explanation would be really great.

Thanks,
Iresh

-----Original Message-----
From: David Carlisle [mailto:davidc@xxxxxxxxx]
Sent: Wednesday, January 24, 2001 4:48 PM
To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
Subject: Re: [xsl] Getting the deepest node and calculating relative
paths




>  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

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


Current Thread