Re: Generate Hierarchical Tree from non-hierarchical XML

Subject: Re: Generate Hierarchical Tree from non-hierarchical XML
From: Michal_Mart@xxxxxxxxxxx
Date: Thu, 17 Aug 2000 15:00:08 -0400

Hi,

This will generate (almost) the  output you require. I don't know if this is the
best way to achieve this output though.
Short explanation: Finds all top-level managers (those with an empty manager
element) output them , for each one find all of their subordinates output them
and recursively process their subordinates. There is also a helper (recursive)
template to output the indenting.
Your required tree listed John 3 before John 4 whereas this one will output John
4 first. I don't know if this makes a difference to you.

Hope this helps,
Michal


<xsl:template match="/">

<xsl:for-each select="employees/employee[manager='']">
     <xsl:value-of select="name"/>
     <xsl:call-template name="myEmployees">
          <xsl:with-param name="empid" select="./id"/>
          <xsl:with-param name="indent" select="0"/>
     </xsl:call-template>
</xsl:for-each>

</xsl:template>

<xsl:template name="myEmployees">
<xsl:param name="empid"/>
<xsl:param name="indent"/>
     <xsl:for-each select="/employees/employee[manager=$empid]">
          <BR/>
          <xsl:call-template name="Indenter">
               <xsl:with-param name="iIndent" select="$indent"/>
          </xsl:call-template>

          <xsl:value-of select="name"/>

          <xsl:call-template name="myEmployees">
          <xsl:with-param name="empid" select="./id"/>
          <xsl:with-param name="indent" select="$indent + 2"/>
          </xsl:call-template>

     </xsl:for-each>
</xsl:template>

<xsl:template name="Indenter">
     <xsl:param name="iIndent"/>
          <xsl:text>&#160;</xsl:text>
          <xsl:if test="$iIndent>0">

          <xsl:call-template name="Indenter">
               <xsl:with-param name="iIndent" select="$iIndent - 1"/>
          </xsl:call-template>

          </xsl:if>

</xsl:template>

</xsl:stylesheet>


Tested and works with IE5 July parser.




Dawinder Khehra <dskhehra@xxxxxxxxx> on 08/17/2000 11:13:23 AM

Please respond to xsl-list@xxxxxxxxxxxxxxxx

To:   xsl-list@xxxxxxxxxxxxxxxx
cc:    (bcc: Michal Mart)

Subject:  Generate Hierarchical Tree from non-hierarchical XML




Here is the XML

<employees>
   <employee>
      <id>1</id>
      <name>John 1</name>
      <manager/>
   </employee>
   <employee>
      <id>4</id>
      <name>John 4</name>
      <manager>1</manager>
   </employee>
   <employee>
      <id>3</id>
      <name>John 3</name>
      <manager>1</manager>
   </employee>
   <employee>
      <id>2</id>
      <name>John 2</name>
      <manager>3</manager>
   </employee>
</employees>

Output needed is
John 1
   John 3
      John 2
   John 4

Please help in writing XSL for ie5.

Thanks!

__________________________________________________
Do You Yahoo!?
Send instant messages & get email alerts with Yahoo! Messenger.
http://im.yahoo.com/


 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