Re: [xsl] Identify last node in nested nodeset with same name

Subject: Re: [xsl] Identify last node in nested nodeset with same name
From: Mukul Gandhi <mukul_gandhi@xxxxxxxxx>
Date: Fri, 24 Jun 2005 01:22:28 -0700 (PDT)
Sorry, I forgot to say ..

The XML file I used was -

<?xml version="1.0"?>
<root>
<menu name="link1"/>
<menu name="link2">
     <menu name="link2a"/>
     <menu name="link2b"/>
</menu>
</root>

Regards,
Mukul

--- Mukul Gandhi <mukul_gandhi@xxxxxxxxx> wrote:

> Hi Mat,
>   Please try this XSL .. It seems to solve the
> problem
> ...
> 
> <?xml version="1.0"?> 
> <xsl:stylesheet
> xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
> version="1.0">
>  
> <xsl:output method="xml" indent="yes" /> 
>  
> <xsl:template match="node() | @*">  
>   <xsl:copy>
>     <xsl:apply-templates select="node() | @*" />
>   </xsl:copy>
> </xsl:template>
>  
> <xsl:template match="root">
>   <ul>
>     <xsl:apply-templates />
>   </ul>  
> </xsl:template>
>  
> <xsl:template match="menu">
>   <xsl:choose>
>     <xsl:when test="child::*">
>       <li><xsl:value-of select="@name" /></li>
>         <ul>
>           <xsl:apply-templates />
>         </ul>  
>       </li>
>     </xsl:when>
>     <xsl:otherwise>
>       <li>
>         <xsl:value-of select="@name" />
>       </li>
>     </xsl:otherwise>
>   </xsl:choose>
> </xsl:template>
>  
> </xsl:stylesheet>
> 
> Regards,
> Mukul
> 
> --- Mat Bergman <matbergman@xxxxxxxxx> wrote:
> 
> > I am working with XML data that shares the same
> > element name for each node set, for example:
> > 
> > <menu name="link1"/>
> > <menu name="link2">
> >      <menu name="link2a"/>
> >      <menu name="link2b"/>
> > </menu>
> > 
> > My XSL stylesheet transforms this into an HTML
> > unordered list, like this:
> > <ul>
> > <li>link1</li>
> > <li>link2
> >      <ul>
> >      <li>link2a</li>
> >      <li>link2b</li>
> >      </ul>
> > </li>
> > </ul>
> > 
> > I can't figure out how to identify the last
> > second-tier node (in this example "link2b") so
> that
> > the stylesheet can write the closing </ul> tag for
> > the
> > nested list. I thought I would reference it with
> > something like <xsl:if test="/menu/menu[last()]">,
> > but
> > my XPath must be incorrect because it fails.
> > 
> > I am currently writing the opening <ul> and
> closing
> > </li> tags for the nested list with this:
> > 
> > <xsl:template match="menu">
> > <xsl:if test="count(menu)>0">
> > <xsl:text>&lt;ul&gt;</xsl:text>
> > </xsl:if>
> > 
> > <xsl:if test="count(menu)=0">
> > <xsl:text>&lt;/li&gt;</xsl:text>
> > </xsl:if>
> > </xsl:template>
> > 
> > If I only knew how to identify the last node in
> > /menu/menu, I could easily write the closing tag.
> > 
> > Thanks,
> > 
> > -Mat
> > 
> > 
> > 
> > 
> > __________________________________________________
> > Do You Yahoo!?
> > Tired of spam?  Yahoo! Mail has the best spam
> > protection around 
> > http://mail.yahoo.com 
> > 
> > 
> 
> 
> 
> 		
> ____________________________________________________
> 
> Yahoo! Sports 
> Rekindle the Rivalries. Sign up for Fantasy Football
> 
> http://football.fantasysports.yahoo.com
> 
> 



		
____________________________________________________ 
Yahoo! Sports 
Rekindle the Rivalries. Sign up for Fantasy Football 
http://football.fantasysports.yahoo.com

Current Thread