[xsl] Re: n-nodes recursive tree

Subject: [xsl] Re: n-nodes recursive tree
From: "Dimitre Novatchev" <dnovatchev@xxxxxxxxx>
Date: Fri, 28 Nov 2003 22:33:01 +0100
> >There isn't any indentation in the presented output.
>
> There is, though it seems to contain a mix of tabs and spaces.
> I saw this (with tabs converted to 8 spaces):
>
> ==========
> In effect, i will never know the max number of levels in tree node, and so
i'd
> like to obtain an html file formated with xsl and wich looks like that:
>
> Tree
>      ---- 1
>              ---- 4
>              ---- 5
>                      ---- 7
>      ---- 2
>              ---- 6
>      ---- 3
> ==========


OK, then the solution is rather straightforward:

This transformation:

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

 <xsl:output method="text"/>
 <xsl:strip-space elements="*"/>

  <xsl:template match="/*">
     <xsl:apply-templates select="node">
       <xsl:with-param name="pstrIndent" select="'&#xA;'"/>
     </xsl:apply-templates>
  </xsl:template>

  <xsl:template match="node">
    <xsl:param name="pstrIndent"/>

    <xsl:variable name="vthisIndent"
         select="concat($pstrIndent, '&#09;')"/>

    <xsl:value-of select="concat($vthisIndent, '----', id)"/>

    <xsl:apply-templates select="node">
      <xsl:with-param name="pstrIndent" select="$vthisIndent"/>
    </xsl:apply-templates>
  </xsl:template>

</xsl:stylesheet>

when applied on the provided source.xml:

<tree>
  <node>
    <id> 1</id>
    <parent> root</parent>
    <node>
      <id> 4</id>
      <parent> 1</parent>
    </node>
    <node>
      <id> 5</id>
      <parent> 1</parent>
      <node>
        <id> 7</id>
        <parent> 5</parent>
      </node>
    </node>
  </node>
  <node>
    <id> 2</id>
    <parent> root</parent>
    <node>
      <id> 6</id>
      <parent> 2</parent>
    </node>
  </node>
  <node>
    <id> 3</id>
    <parent> root</parent>
  </node>
</tree>

produces the wanted result:


 ---- 1
  ---- 4
  ---- 5
   ---- 7
 ---- 2
  ---- 6
 ---- 3


Hope this helped.



=====
Cheers,

Dimitre Novatchev.
http://fxsl.sourceforge.net/ -- the home of FXSL




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


Current Thread