Re: [xsl] Recursion Examples

Subject: Re: [xsl] Recursion Examples
From: Dan Diebolt <dandiebolt@xxxxxxxxx>
Date: Mon, 30 Apr 2001 22:05:43 -0700 (PDT)
>I need to be able to build a tree that represents the data

XML is basically a tree from the get go, so it is a little
unclear what 'tree' you want to build. I interpreted you problem
to basically ask how to build the containment hierarchy
of modules that included other modules based on the relationship
joining <id> to <parentid>. Also, I fleshed out your input XML 
to be three-deep and three-wide versus your original four-deep
and one-wide example. To show the module containment clearly, I 
pushed your <id> and <name> tags into attributes.

Regards,

Dan
-------------------------------------------
File: VersionModule20APril2001.out
<?xml version="1.0" encoding="utf-8"?>
<module id="0" name="foo">
   <module id="1" name="bar">
      <module id="4" name="saz"/>
      <module id="5" name="war"/>
      <module id="6" name="doo"/>
   </module>
   <module id="2" name="baz">
      <module id="7" name="daz"/>
      <module id="8" name="diz"/>
      <module id="9" name="wat"/>
   </module>
   <module id="3" name="bat">
      <module id="a" name="yat"/>
      <module id="b" name="yoo"/>
      <module id="c" name="sor"/>
   </module>
</module>

File: VersionModule20APril2001.xml
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="VersionModule20APril2001.xsl"?>

<version>
 <module><id>0</id><name>foo</name><parentid>root</parentid></module>
 <module><id>1</id><name>bar</name><parentid>0</parentid></module>
 <module><id>2</id><name>baz</name><parentid>0</parentid></module>
 <module><id>3</id><name>bat</name><parentid>0</parentid></module>
 <module><id>4</id><name>saz</name><parentid>1</parentid></module>
 <module><id>5</id><name>war</name><parentid>1</parentid></module>
 <module><id>6</id><name>doo</name><parentid>1</parentid></module>
 <module><id>7</id><name>daz</name><parentid>2</parentid></module>
 <module><id>8</id><name>diz</name><parentid>2</parentid></module>
 <module><id>9</id><name>wat</name><parentid>2</parentid></module>
 <module><id>a</id><name>yat</name><parentid>3</parentid></module>
 <module><id>b</id><name>yoo</name><parentid>3</parentid></module>
 <module><id>c</id><name>sor</name><parentid>3</parentid></module>
</version>

File: VersionModule20APril2001.xsl 
<?xml version="1.0"?>

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
   
 <xsl:output indent="yes"/>
 
 <xsl:template match="/">
  <xsl:apply-templates select="version"/>
 </xsl:template>
   
 <xsl:template match="version">
  <xsl:apply-templates select="module[parentid='root']">
   <xsl:with-param name="ParentId" select="'root'"/>
  </xsl:apply-templates>
 </xsl:template>
 
 <xsl:template match="module">
  <xsl:param name="ParentId"/>
  <xsl:variable name="Id" select="id"/>
  <xsl:copy>
   <xsl:attribute name="id"><xsl:value-of select="id"/></xsl:attribute>
   <xsl:attribute name="name"><xsl:value-of select="name"/></xsl:attribute>
   <xsl:apply-templates select="parent::*/module[parentid=$Id]">
    <xsl:with-param name="ParentId" select="parentid"/>
   </xsl:apply-templates>
  </xsl:copy>
 </xsl:template>
</xsl:stylesheet>

__________________________________________________
Do You Yahoo!?
Yahoo! Auctions - buy the things you want at great prices
http://auctions.yahoo.com/

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


Current Thread