xsl-list
[Top] [All Lists]

Re: [xsl] How to make tree menu from flat XML

2006-07-23 07:32:30
If the levels would be limited to 3 only (or can there be any number
of levels?), then the following stylesheet would work:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>

<xsl:output method="xml" indent="yes"/>
        
<xsl:template match="/records">
 <records>
   <xsl:for-each select="record[parentkeynum = 0]">
     <record>
       <xsl:variable name="key1" select="keynum" />
       <xsl:copy-of select="*" />
       <xsl:for-each select="../record[parentkeynum = $key1]">
         <record>
           <xsl:variable name="key2" select="keynum" />
           <xsl:copy-of select="*" />
           <xsl:for-each select="../record[parentkeynum = $key2]">
             <record>
               <xsl:copy-of select="*" />
             </record>
           </xsl:for-each>
         </record>
       </xsl:for-each>
     </record>
   </xsl:for-each>
 </records>
</xsl:template>
        
</xsl:stylesheet>

Regards,
Mukul

http://gandhimukul.tripod.com

On 7/23/06, Radoslav Kolarov <roonex(_at_)yahoo(_dot_)com> wrote:
Mukul thanks for the answer. The problem is how to
turn tree structured information into dynamic tree
menu. I found some js scripts but I can use them only
if XML is structured heirarchy. So I have to transform
XML to this:

<?xml version="1.0" ?>
- <records>

- <record>
 <keynum>-100000</keynum>
 <keyname>FINANCIAL ALERTS (5)</keyname>
 <parentkeynum>0</parentkeynum>
 <rowcnt>5</rowcnt>
 <balance>0</balance>
 - <record>
   <keynum>-1</keynum>
   <keyname>2 CLIENTS HAVE A NEGATIVE
BALANCE</keyname>
   <parentkeynum>-100000</parentkeynum>
   <rowcnt>2</rowcnt>
   <balance>0</balance>
   - <record>
     <keynum>35</keynum>
     <keyname>35 MR. MICHAEL NOLAN</keyname>
     <parentkeynum>-1</parentkeynum>
     <rowcnt>1</rowcnt>
     <balance>-275</balance>
     </record>
   - <record>
     <keynum>142</keynum>
     <keyname>142 MR. JOHN CALINSKI</keyname>
     <parentkeynum>-1</parentkeynum>
     <rowcnt>1</rowcnt>
     <balance>-11</balance>
     </record>


   </record>

 </record>

</records>

Where the elements witn parentkeynum=0 to be roots,
and elements with parentkeynum=root keynum to be their
childrens nodes, and same thing for third level. So
the question now is how to transform flat XML to
heirarchy XML file like this...

--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--