我正在尝试构建一个理论上可以使用 XSL 无限递归的导航。不幸的是,我在这个领域的技能仍在培养中。谁能告诉我这段代码哪里出错了?
这里的关键是导航的前 2 级有些独特(类名等),但在第 3 级之后,导航几乎是一遍又一遍嵌套的相同元素。
更新: 在导航级别 2 之后没有递归,在级别 2 之后输出不存在,我不知道如何应用递归。我觉得这应该更容易,但我在 XSL 方面的技能并不是那么好。
HTML(我们需要它来生成):
<ul class="nav-l1">
<li class="nav-active"><a href="nav2.html" class="nav-item trigger-chan nav-next"><span><span class="trigger-cntr">First Level Parent</span></span></a>
<ul class="nav-l2 nav-hidden">
<li class="nav-active"><a href="nav2.html" class="nav-item"><span>Overview</span></a></li>
<li><a href="#" class="nav-item trigger-chan nav-next"><span><span class="trigger-cntr">Second Level Parent</span></span></a>
<ul class="nav-ls nav-hidden">
<li><a href="#" class="nav-item"><span>Third Level</span></a></li>
<li><a href="#" class="nav-item"><span>Third Level</span></a></li>
</ul>
</li>
<li><a href="#" class="nav-item"><span>Second Level</span></a></li>
</ul>
</li>
<li><a href="#" class="nav-item trigger-chan nav-next"><span><span class="trigger-cntr">First Level Parent</span></span></a>
<!-- 2nd level of navigation. -->
<ul class="nav-l2 nav-hidden">
<li><a href="#" class="nav-item"><span>Overview</span></a></li>
<li><a href="#" class="nav-item trigger-chan nav-next"><span><span class="trigger-cntr">Second Level Parent</span></span></a>
<!-- 3rd level of navigation. -->
<ul class="nav-ls nav-hidden">
<li><a href="#" class="nav-item"><span>Third Level</span></a></li>
<li><a href="#" class="nav-item"><span>Third Level</span></a></li>
<li><a href="#" class="nav-item"><span>Third Level</span></a></li>
</ul>
</li>
<li><a href="#" class="nav-item"><span>Second Level</span></a></li>
<li><a href="#" class="nav-item"><span>Second Level</span></a></li>
</ul>
</li>
</ul>
示例 XML(初始数据馈送的格式):
<data>
<folders level="1">
<folder clickable="Y" url="/lorem/ipsum.html" name="Lorem Ipsum"/>
<folder clickable="Y" url="/level/one.html" name="Level One"/>
<folder clickable="Y" url="/foo/bar.html" name="Foo Bar">
<folders level="2">
<folder clickable="Y" url="/level/two.html" name="Level two"/>
<folder clickable="Y" url="/child/item.html" name="Child Item">
<folders level="3">
<folder clickable="Y" url="/child/child/item.html" name="Child's Child Item">
<folders level="4">
<folder clickable="Y" url="/destiny/child/item.html" name="Destiny's Child"/>
</folders>
</folder>
</folders>
</folder>
</folders>
</folder>
</folders>
</data>
XSL(我们需要转换 XML):
<?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" omit-xml-declaration="yes" indent="yes"/>
<xsl:template match="/">
<xsl:choose>
<xsl:when test="/data/folders">
<!-- NAVIGATION BEGINS HERE -->
<ul class="nav-l1">
<!-- LEVEL 1 -->
<xsl:for-each select="/data/folders[@level=1]/folder[not (@clickable ) or @clickable ='Y' ]">
<li>
<xsl:if test="@selected='Y'">
<xsl:attribute name="class">nav-active</xsl:attribute>
</xsl:if>
<a href="{@url}" class="nav-item">
<xsl:choose>
<xsl:when test="child::*">
<xsl:attribute name="class">trigger-chan nav-next</xsl:attribute>
<span>
<span class="trigger-cntr">
<xsl:value-of select="@name"/>
</span>
</span>
</xsl:when>
<xsl:otherwise>
<span><xsl:value-of select="@name" /></span>
</xsl:otherwise>
</xsl:choose>
</a>
<!-- LEVEL 2 -->
<xsl:choose>
<xsl:when test="child::*">
<ul class="nav-l2 nav-hidden">
<xsl:for-each select="folders[@level=2]/folder[not (@clickable ) or @clickable ='Y' ]">
<li>
<xsl:if test="@selected='Y'">
<xsl:attribute name="class">nav-active</xsl:attribute>
</xsl:if>
<a href="{@url}" class="nav-item">
<xsl:choose>
<xsl:when test="child::*">
<xsl:attribute name="class">trigger-chan nav-next</xsl:attribute>
<span>
<span class="trigger-cntr">
<xsl:value-of select="@name"/>
</span>
</span>
</xsl:when>
<xsl:otherwise>
<span><xsl:value-of select="@name" /></span>
</xsl:otherwise>
</xsl:choose>
</a>
<!-- LEVEL 3 and beyond ... -->
<xsl:apply-templates />
</li>
</xsl:for-each>
</ul>
</xsl:when>
<xsl:otherwise>
</xsl:otherwise>
</xsl:choose>
</li>
</xsl:for-each>
</ul>
</xsl:when>
</xsl:choose>
</xsl:template>
<!-- Infinity and beyond (=> Level 3) -->
<xsl:template name="folder">
<ul class="nav-ls nav-hidden">
<li>
<xsl:if test="@selected='Y'">
<xsl:attribute name="class">nav-active</xsl:attribute>
</xsl:if>
<a href="{@url}" class="nav-item">
<xsl:choose>
<xsl:when test="child::*">
<xsl:attribute name="class">trigger-chan nav-next</xsl:attribute>
<span>
<span class="trigger-cntr">
<xsl:value-of select="@name"/>
</span>
</span>
</xsl:when>
<xsl:otherwise>
<span><xsl:value-of select="@name" /></span>
</xsl:otherwise>
</xsl:choose>
</a>
<xsl:apply-templates select="folders[@level>3]/folder" />
</li>
</ul>
</xsl:template>
</xsl:stylesheet>
任何帮助是极大的赞赏。
谢谢!