[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: Getting the deepest node and calculating relative

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

>  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

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.