Re: [xsl] XSL to create nested list items?

Subject: Re: [xsl] XSL to create nested list items?
From: Shawn <sgrover@xxxxxxxxxxxxxx>
Date: Fri, 19 Nov 2004 03:56:43 -0700
Thank you for the response Jarno.

I tried your template sample, but kept getting errors on the first 
xsl:template line.  That's PHP for you though, it's VERY picky I'm finding, 
and I don't see anything in the line that would trigger an error.

I did manage to resolve the problem though.  I took my clue from your "menu" 
template, and kicked myself for not seeing the simpler way sooner.  Instead 
of simply putting a new <item> element inside an existing one, I wrapped the 
sub-menu items in a <menu> element.  Then, with the XSL, I could write a 
template for the <menu> elements that would create the <ul> tags... er, 
here's the new xsl - I think it's easier to see what I did than me trying to 
explain it:

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
	
	<xsl:template match="/">
		<xsl:apply-templates/>
	</xsl:template>
	
	<xsl:template match="menu">
		<ul>
			<xsl:apply-templates/>
		</ul>
	</xsl:template>
  
	<xsl:template match="item">
		<li>
			<a href="{url}">
				<xsl:value-of select="name"/>
			</a>
		</li>
		<xsl:apply-templates select="menu"/>
	</xsl:template>

</xsl:stylesheet>

with the xml looking like so:

<menu>
	<item>
		<name>Biographies</name>
		<url>#</url>
		<menu>
			<item>
				<name>Chatter Box</name>
				<url>#</url>
			</item>
		</menu>
	</item>
	<item>
		<name>Media</name>
		<url>#</url>
		<menu>
			<item>
				<name>Audio</name>
				<url>#</url>
				<menu>
					<item>
						<name>Song 1</name>
						<url>#</url>
					</item>
					<item>
						<name>Song 2</name>
						<url>#</url>
					</item>
				</menu>
			</item>
			<item>
				<name>Images</name>
				<url>#</url>
			</item>
		</menu>
	</item>	
</menu>

I'm getting the output I was after now.  Now I can focus on the CSS side of 
things and make my menu all pretty.. :)

Thanks again.

Shawn

On Friday 19 November 2004 02:59, Jarno.Elovirta@xxxxxxxxx wrote:
>   <xsl:template match="menu">
>     <html>
>       <head>
>         <title></title>
>       </head>
>       <body>
>         <ul>
>           <xsl:apply-templates select="item"/>
>         </ul>
>       </body>
>     </html>
>   </xsl:template>
>   <xsl:template match="item">
>     <li>
>       <a href="{url}">
>         <xsl:value-of select="name"/>
>       </a>
>       <xsl:if test="item">
>         <ul>
>           <xsl:apply-templates select="item"/>
>         </ul>
>       </xsl:if>
>     </li>
>   </xsl:template>
>
>
> But if you have multiple top-level items, do they all have @parent = 0? How
> do you know which item is inside which. I think you're better off using the
> earlier XML vocabulary. If you want to add numbering, add
>
>   <xsl:number level="multiple"/>
>
> as the first child of li element, just before a element.
>
> Cheers,
>
> Jarno

Current Thread