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

RE: Creating a hierarchal unordered list from flat fil

Subject: RE: Creating a hierarchal unordered list from flat file
From: Adam Van Den Hoven <Adam.Hoven@xxxxxxxxxxxx>
Date: Thu, 25 Jan 2001 14:04:06 -0800
flat hierarchal
David:

I have a solution to your problem...

Given that your source Document is: (I had to fix it)
<Root>
  <Groups GroupID="1" Name="Group1" ParentID="0"/>
  <Groups GroupID="2" Name="Group2" ParentID="1"/>
  <Groups GroupID="3" Name="Group3" ParentID="1"/>
  <Groups GroupID="4" Name="Group4" ParentID="1"/>
  <Groups GroupID="5" Name="Group5" ParentID="2"/>
  <Groups GroupID="6" Name="Group6" ParentID="2"/>
  <Groups GroupID="7" Name="Group7" ParentID="3"/>
  <Groups GroupID="8" Name="Group8" ParentID="3"/>
  <Groups GroupID="9" Name="Group9" ParentID="4"/>
  <Groups GroupID="10" Name="Group10" ParentID="5"/>
  <Groups GroupID="11" Name="Group11" ParentID="5"/>
  <Groups GroupID="12" Name="Group12" ParentID="6"/>
  <Groups GroupID="13" Name="Group13" ParentID="8"/>
  <Groups GroupID="14" Name="Group14" ParentID="8"/>
  <Groups GroupID="15" Name="Group15" ParentID="9"/>
  <Groups GroupID="16" Name="Group16" ParentID="10"/>
  <Groups GroupID="17" Name="Group17" ParentID="10"/>
  <Groups GroupID="18" Name="Group18" ParentID="16"/>
  <Groups GroupID="19" Name="Group19" ParentID="18"/>
  <Groups GroupID="20" Name="Group20" ParentID="19"/>
</Root>

If you apply the following stylesheet:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
   <xsl:template match="/">
     <root>
         <xsl:apply-templates select="/Root/Groups[@ParentID=0]"/>
      </root>
   </xsl:template>
   <xsl:template match="Groups">
      <xsl:copy>
         <xsl:apply-templates select="@*"/>
         <xsl:apply-templates
select="../Groups[@ParentID=current()/@GroupID]"/>
      </xsl:copy>
   </xsl:template>
   <xsl:template match="@*">
      <xsl:copy/>
   </xsl:template>
</xsl:stylesheet>

You should get the following results:
<root>
   <Groups GroupID="1" Name="Group1" ParentID="0">
      <Groups GroupID="2" Name="Group2" ParentID="1">
         <Groups GroupID="5" Name="Group5" ParentID="2">
            <Groups GroupID="10" Name="Group10" ParentID="5">
               <Groups GroupID="16" Name="Group16" ParentID="10">
                  <Groups GroupID="18" Name="Group18" ParentID="16">
                     <Groups GroupID="19" Name="Group19" ParentID="18">
                        <Groups GroupID="20" Name="Group20" ParentID="19"/>
                     </Groups>
                  </Groups>
               </Groups>
               <Groups GroupID="17" Name="Group17" ParentID="10"/>
            </Groups>
            <Groups GroupID="11" Name="Group11" ParentID="5"/>
         </Groups>
         <Groups GroupID="6" Name="Group6" ParentID="2">
            <Groups GroupID="12" Name="Group12" ParentID="6"/>
         </Groups>
      </Groups>
      <Groups GroupID="3" Name="Group3" ParentID="1">
         <Groups GroupID="7" Name="Group7" ParentID="3"/>
         <Groups GroupID="8" Name="Group8" ParentID="3">
            <Groups GroupID="13" Name="Group13" ParentID="8"/>
            <Groups GroupID="14" Name="Group14" ParentID="8"/>
         </Groups>
      </Groups>
      <Groups GroupID="4" Name="Group4" ParentID="1">
         <Groups GroupID="9" Name="Group9" ParentID="4">
            <Groups GroupID="15" Name="Group15" ParentID="9"/>
         </Groups>
      </Groups>
   </Groups>
</root>

There are a few things that you may need to think about. Currently a root
node of 0 is a fixed value. It should be possible to use a variable to
figure out which ParentID refers to the root node. Its simply a matter of
finding the ParentID for which there is no matching GroupID. Given that you
may have some errors and accidentally include two ParentIDs for which there
are no GroupIDs you may need to be a little devious about how you make the
choice...

But it works. 
Adam

 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.