I have a problem with a bit of XSLT that is parsing a submenu twice. What I need is this:
But what I'm actually getting is this:
You can see the XSLT below -- I think the problem is in the very last xsl:template, but sure I am not ... I'd appreciate your help!
Code:
<ul id="meny">
<li><a href="">Menu 1</a></li>
<li><a href="">Menu 2</a>
<ul>
<li><a href="">Menu 2-1</a></li>
<li><a href="">Menu 2-2</a></li>
</ul>
</li>
<li><a href="">Menu 3</a></li>
</ul>
But what I'm actually getting is this:
Code:
<ul id="meny">
<li><a href="">Menu 1</a></li>
<li><a href="">Menu 2</a>
<ul>
<li><a href="">Menu 2-1</a></li>
<li><a href="">Menu 2-2</a></li>
</ul>
</li>
<li><a href="">Menu 2-1</a></li>
<li><a href="">Menu 2-2</a></li>
<li><a href="">Menu 3</a></li>
</ul>
You can see the XSLT below -- I think the problem is in the very last xsl:template, but sure I am not ... I'd appreciate your help!
Code:
<?xml version='1.0'?>
<xsl:stylesheet xmlns:xsl="[URL unfurl="true"]http://www.w3.org/1999/XSL/Transform"[/URL] version="1.0">
<xsl:output method="html" indent="yes" encoding="ISO-8859-1"/>
<!--
==========================================================================
Start menu templates, be careful when modifying
==========================================================================
-->
<xsl:param name="PATH">
<xsl:call-template name="TRACE_FOLDER_TO_ROOT">
<!-- Specify id below to tell script what folder ID to use as basis for menu location -->
<xsl:with-param name="id" select="(//ARTICLES/ACTION/FOLDER_CONTENTS/PARENT/FOLDER/ID|//ARTICLES/ACTION/ARTICLE/FOLDER_ID)[1]"/>
</xsl:call-template>
</xsl:param>
<xsl:template name="TRACE_FOLDER_TO_ROOT">
<!-- Trace current folder ID to top folder -->
<xsl:param name="id"/>
<xsl:param name="str"/>
<xsl:variable name="parent" select="//ARTICLES//FOLDERS/FOLDER[ID=$id]/PARENT"/>
<xsl:variable name="newstr" select="concat($str, ',', $id, ',')"/>
<xsl:choose>
<xsl:when test="$parent and //ARTICLES//FOLDERS/FOLDER[ID=$parent]">
<!-- Recurse -->
<xsl:call-template name="TRACE_FOLDER_TO_ROOT">
<xsl:with-param name="id" select="$parent"/>
<xsl:with-param name="str" select="$newstr"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<!-- We're at top -->
<xsl:value-of select="$newstr"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template match="FOLDERS/FOLDER" mode="MENU">
<xsl:if test="not(PARENT) or contains($PATH, concat(',', PARENT, ','))">
<!-- We're either at root or have a parent in path -->
<xsl:apply-templates select="." mode="WRITE"/>
</xsl:if>
<xsl:if test="contains($PATH, concat(',', ID, ','))">
<!-- We're a parent in path -->
<xsl:variable name="myID" select="ID"/>
<xsl:apply-templates select="../FOLDER[PARENT=$myID]" mode="MENU"/>
</xsl:if>
</xsl:template>
<!--
==========================================================================
End menu templates
==========================================================================
-->
<!-- Override this to implement own content -->
<xsl:template name="MENU">
<ul id="meny">
<xsl:apply-templates select="//FOLDERS/FOLDER[DEPTH=1]" mode="MENU"/>
</ul>
</xsl:template>
<xsl:template match="FOLDERS/FOLDER[DEPTH=1]" mode="WRITE">
<li><a href="{URL}"><xsl:value-of select="NAME"/></a>
<xsl:if test="../*[PARENT = current()/ID] and contains($PATH, concat(',', ID, ','))">
<ul>
<xsl:apply-templates select="//FOLDERS/FOLDER[DEPTH=2]" mode="MENU"/>
</ul>
</xsl:if>
</li>
</xsl:template>
<xsl:template match="FOLDERS/FOLDER[DEPTH=2]" mode="WRITE">
<li><a href="{URL}"><xsl:value-of select="NAME"/></a></li>
</xsl:template>
</xsl:stylesheet>