Altova Mailing List Archives

Re: [xsl] Hierarchy based on indentation (long)

From: Grainne Reilly <greilly1@--------->
Date: 12/9/2002 9:29:00 PM
Thanks Dimitre for this approach which certainly is more comprehendible 
than my original.  I can't rely on the number of spaces per indentation 
level being consistent, I can only rely on each indentation level having 
the same number of spaces. I have made an attempt to adapt your code to 
cover that (see below).


<xsl:stylesheet version="1.0"
extension-element-prefixes="exsl saxon">
<xsl:output omit-xml-declaration="yes" indent="yes"/>

<xsl:template match="/">

          <xsl:variable name="vSortedRows">

                <xsl:apply-templates select="/*/row" mode="sort">

                        <xsl:sort data-type="number" 



        <xsl:variable name="vtLevels">

                <xsl:call-template name="getLevels">

                        <xsl:with-param name="pSortedRows" 



           <xsl:variable name="vLevels" select="exsl:node-set($vtLevels)"/>

        <xsl:for-each select="/*/row">

                <xsl:variable name="vThisIndent" 


                        <xsl:call-template name="genDescription">

                        <xsl:with-param name="pNode" select="."/>

                                <xsl:with-param name="pRow" 

                                <xsl:with-param name="pLevels" 

                                <xsl:with-param name="pLevel" 
select="$vLevels/level[@indent = $vThisIndent]/@num"/>





<xsl:template match="row" mode="sort">
        <xsl:copy-of select="."/>

<xsl:template name="getLevels">

        <xsl:param name="pSortedRows" select="/.."/>

        <xsl:param name="pLevelNumber" select="1"/>

        <xsl:variable name="vThisIndent" 

        <xsl:variable  name="vNext" 
1,1))) > $vThisIndent ]" />

        <level num="{$pLevelNumber}" indent="{$vThisIndent}"/>

        <xsl:if test="$vNext">

                <xsl:call-template name="getLevels">

                        <xsl:with-param name="pSortedRows" select="$vNext"/>

                        <xsl:with-param name="pLevelNumber" 
select="$pLevelNumber + 1"/>




<xsl:template name="genDescription">
    <xsl:param name="pNode" select="/.."/>
    <xsl:param name="pRow" />
    <xsl:param name="pLevel"/>
    <xsl:param name="pLevels" select="/.."/>

  <xsl:if test="$pLevel > 1">

    <xsl:variable name="vindentParent" select="$pLevels/level[@num = 
($pLevel - 1)]/@indent"/>

    <xsl:variable name="vParent"

     select="preceding-sibling::row[ cell[ 
string-length(substring-before(.,substring(normalize-space(.), 1,1))) = 
$vindentParent ] ][1]"/>

    <xsl:call-template name="genDescription">
      <xsl:with-param name="pNode" select="$vParent"/>
      <xsl:with-param name="pRow" select="$pRow"/>
      <xsl:with-param name="pLevel" select="$pLevel - 1"/>
      <xsl:with-param name="pLevels" select="$pLevels"/>

  <description row="{$pRow}" level="{$pLevel}">
    <xsl:value-of select="$pNode/cell"/>




Hi Grainne,

It seems to me that the following solution is simpler. It uses a
recursively called named template to generate the descriptions, and
also the fact that the indent for every next level is 3:



Dimitre Novatchev. -- the home of FXSL

Do you Yahoo!?
Yahoo! Mail Plus - Powerful. Affordable. Sign up now.

 XSL-List info and archive:

XSL-List info and archive:


These Archives are provided for informational purposes only and have been generated directly from the Altova mailing list archive system and are comprised of the lists set forth on Therefore, Altova does not warrant or guarantee the accuracy, reliability, completeness, usefulness, non-infringement of intellectual property rights, or quality of any content on the Altova Mailing List Archive(s), regardless of who originates that content. You expressly understand and agree that you bear all risks associated with using or relying on that content. Altova will not be liable or responsible in any way for any content posted including, but not limited to, any errors or omissions in content, or for any losses or damage of any kind incurred as a result of the use of or reliance on any content. This disclaimer and limitation on liability is in addition to the disclaimers and limitations contained in the Website Terms of Use and elsewhere on the site.