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

Subject: RE: [xsl] Identify last node in nested nodeset with same name
From: "Aron Bock" <aronbock@xxxxxxxxxxx>
Date: Fri, 24 Jun 2005 04:33:48 +0000
Mat,

Luckily, there's an easier and more "natural" way to do this -- don't write "tags", as you do, but create full-fledged elements. Using this, with recursion, yields a simple approach.

With this input:

<data>
<menu name="link1"/>
<menu name="link2">
     <menu name="link2a"/>
     <menu name="link2b"/>
</menu>
</data>

This transform, using call-template:

<?xml version="1.0" encoding="iso8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
<xsl:output method="xml" indent="yes"/>
<xsl:strip-space elements="*"/>


   <xsl:template match="/data">
       <xsl:call-template name="write-menu">
           <xsl:with-param name="items" select="menu"/>
       </xsl:call-template>

</xsl:template>

   <xsl:template name="write-menu">
       <xsl:param name="items" select="/.."/>
       <ul>
           <xsl:for-each select="$items">
               <li>
                   <xsl:value-of select="@name"/>
                   <xsl:if test="menu">
                       <xsl:call-template name="write-menu">
                           <xsl:with-param name="items" select="menu"/>
                       </xsl:call-template>
                   </xsl:if>
               </li>
           </xsl:for-each>
       </ul>
   </xsl:template>
</xsl:stylesheet>


Produces:


<?xml version="1.0" encoding="UTF-8"?>
<ul>
 <li>link1</li>
 <li>link2<ul>
     <li>link2a</li>
     <li>link2b</li>
   </ul>
 </li>
</ul>


As an aside, if iterating over a set of nodes, last() in a test can identify the last node.


Regards,

--A




From: Mat Bergman <matbergman@xxxxxxxxx>
Reply-To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
Subject: [xsl] Identify last node in nested nodeset with same name
Date: Thu, 23 Jun 2005 19:26:19 -0700 (PDT)

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

_________________________________________________________________
Dont just search. Find. Check out the new MSN Search! http://search.msn.click-url.com/go/onm00200636ave/direct/01/


Current Thread