RE: [xsl] Recursively Generating Menu and Sub-Menu items

Subject: RE: [xsl] Recursively Generating Menu and Sub-Menu items
From: "Gabriel Osorio" <gosorio@xxxxxxxxxxx>
Date: Fri, 16 Feb 2007 21:07:19 -0500
Good exercise!!

<?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" omit-xml-declaration="yes" />
	<xsl:template match="item">
		<ul>
			<li>
				<xsl:value-of select="name" />
			</li>
			<xsl:apply-templates select="item" />
		</ul>
	</xsl:template>
</xsl:stylesheet>


-----Original Message-----
From: Rashmi Rubdi [mailto:dev_subscriptions@xxxxxxxxx] 
Sent: Friday, February 16, 2007 7:50 PM
To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
Subject: [xsl] Recursively Generating Menu and Sub-Menu items

Hello Everyone, 

I'm still learning XSLT while also learning other languages so it has been a
little slow for me to pick things up with each language.

I did some searching on my problem, and came accross a few similar threads
but the code they demostrated was rather large and contained unrelated code
with it, so it was a little difficult to understand the desired solution.

I want to recursively generate nested menus with XSLT with the menu
structured inside an XML. 
I have a sample XML structure which is supposed to represent a Menu
hierarchy as follows:

(Please note that I'm flexible with any other XML structure that represents
a nested menu, if it helps to improves the XSLT solution)

<?xml version="1.0" encoding="UTF-8"?>
<item>
    <name>Main Menu</name>
    <item>
        <name>First Level Item</name>
        <item>
            <name>Second Level Item</name>
            <item>
                <name>Third Level Item</name>
            </item>
        </item>
    </item>
    <item>
        <name>ABC</name>
        <item>
            <name>XYZ</name>
        </item>
        <item>
            <name>PQR</name>
        </item>
        <item>
            <name>LMN</name>
        </item>
    </item>
</item>

The desired output is in the form of an HTML nested list as follows:

<ul>
     <li>Main Menu
        <ul>
            <li>First Level Item
                <ul>
                    <li>Second Level Item
                        <ul>
                            <li>Third Level Item</li>
                        </ul>
                    </li>
                </ul>
            </li>
            <li>ABC
                <ul>
                    <li>XYZ</li>
                    <li>PQR</li>
                    <li>LMN</li>
                </ul>
            </li>
        </ul>
     </li>
</ul>

I have tried to generate the above output with the following XSLT 1.0 :

<?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" omit-xml-declaration="yes"/>

    <xsl:template match="/">
        <xsl:apply-templates select="item"/>
    </xsl:template>

    <xsl:template match="item">
        <ul>
            <xsl:apply-templates select="name"/>
        </ul>
    </xsl:template>

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

</xsl:stylesheet>

but the above xslt generates this ouput: <ul><li/></ul> 

Any help is appreciated.

-Regards
Rashmi



 
____________________________________________________________________________
________
Need Mail bonding?
Go to the Yahoo! Mail Q&A for great tips from Yahoo! Answers users.
http://answers.yahoo.com/dir/?link=list&sid=396546091

Current Thread