XML Editor
Sign up for a WebBoard account Sign Up Keyword Search Search More Options... Options
Chat Rooms Chat Help Help News News Log in to WebBoard Log in Not Logged in
Show tree view Topic
Topic Page 1 2 3 4 5 6 7 8 9 Go to previous topicPrev TopicGo to next topicNext Topic
Postnext
Julian GuySubject: XSL help needed for recursive nested list menu
Author: Julian Guy
Date: 27 Feb 2006 04:41 AM
I have an XML file like this:

<menu>
<page id="1" name="home" url="default.asp">
<page id="2" name="second level 1" url="">
<page id="5" name="third level 1" url="">
<page id="8" name="fourth level 1" url=""/>
<page id="9" name="fourth level 2" url=""/>
</page>
<page id="6" name="third level 2" url=""/>
<page id="7" name="third level 3" url=""/>
</page>
<page id="3" name="second level 2" url=""/>
<page id="4" name="second level 3" url=""/>
</page>
</menu>

I need to write an XSL that writes out an unordered HTML list for the section based on a parameter of the current page. So. If i'm on page with id="9" I need:

Code:

<ul>
<li class="parent"><a href="">second level 1</a>
<li class="parent"><a href="">third level 1</a>
<li><a href="">fourth level 1</a>
<li class="selected"><a href="">fourth level 2</a>
</li>
<li/><a href="">third level 2</a></li>
<li/><a href="">third level 3</a></li>
</li>
</ul>



I'm really stuck. I've got this far:

Code:

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" indent="yes"/>

<xsl:param name="pageId" />

<xsl:template match="page">
<ul>
<xsl:choose>
<xsl:when test="descendant-or-self::page[@id=$pageId]">

<li class="parent"><xsl:value-of select="@name"/><xsl:apply-templates select="page"/></li>
</xsl:when>
<xsl:otherwise>

<li><xsl:value-of select="@name"/><xsl:apply-templates select="page"/></li>
</xsl:otherwise>
</xsl:choose>
</ul>
</xsl:template>

</xsl:stylesheet>



Nearly but not quite. can anyone tell me how to limit it to match within a specified depth?

Posttop
Minollo I.Subject: XSL help needed for recursive nested list menu
Author: Minollo I.
Date: 27 Feb 2006 09:59 AM
Julian,
I'm not sure I understand your constraints.

Does something like this work any better for you?
<xsl:choose>
<xsl:when test="descendant-or-self::page[@id=$pageId]">
<li class="parent">
<xsl:value-of select="@name"/>
<xsl:apply-templates select="page"/>
</li>
</xsl:when>
<xsl:when test="preceding-sibling::page/page[@id=$pageId] or following-sibling::page/page[@id=$pageId]">
<li class="parent">
<xsl:value-of select="@name"/>
<xsl:apply-templates select="page"/>
</li>
</xsl:when>
<xsl:otherwise>
<xsl:apply-templates select="page"/>
</xsl:otherwise>
</xsl:choose>

From what you wrote, I couldn't understand what you are try to show/hide when specifying a specific page id, and if what you want to display is a flat or hierarchical list.

Minollo

 
Topic Page 1 2 3 4 5 6 7 8 9 Go to previous topicPrev TopicGo to next topicNext Topic
Download A Free Trial of Stylus Studio 6 XML Professional Edition Today! Powered by Stylus Studio, the world's leading XML IDE for XML, XSLT, XQuery, XML Schema, DTD, XPath, WSDL, XHTML, SQL/XML, and XML Mapping!  
go

Log In Options

Site Map | Privacy Policy | Terms of Use | Trademarks
Stylus Scoop XML Newsletter:
W3C Member
Stylus Studio® and DataDirect XQuery ™are from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2016 All Rights Reserved.