1

我正在尝试使用 XSLT 为 Umbraco 创建一个垂直的多级导航,它将自动添加嵌套列表。目前,我不得不继续手动将每个嵌套列表添加到 XSLT 中(这不是一个好习惯)。

我创建了以下菜单,但我需要帮助,因为它没有正确创建嵌套列表:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xsl:stylesheet [ <!ENTITY nbsp "&#x00A0;"> ]>
<xsl:stylesheet
  version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:msxml="urn:schemas-microsoft-com:xslt"
  xmlns:umbraco.library="urn:umbraco.library"
  exclude-result-prefixes="msxml umbraco.library">

<xsl:output method="xml" omit-xml-declaration="yes"/>

<xsl:param name="currentPage"/>

<xsl:template match="/">
<xsl:variable name="level1" select="$currentPage/ancestor-or-self::* [@level = 2 and string(umbracoNaviHide) != '1' and string(hideFromSideNavigation) != '1']" />  
<xsl:variable name="level" select="2"/>

<div class="subnav">
    <!-- LEVEL 1 -->
    <p><xsl:value-of select="$level1/@nodeName"/></p>

    <ul>
        <xsl:call-template name="drawNodes">
            <xsl:with-param name="parent" select="$currentPage/ancestor-or-self::* [@isDoc][@level=2]"/>
        </xsl:call-template>
    </ul>
</div>
</xsl:template>

<xsl:template name="drawNodes">
    <xsl:param name="parent"/>
    <xsl:for-each select="$parent/ancestor-or-self::* [@level=2]/* [@isDoc and string(umbracoNaviHide) != '1' and string(@template) != '0']">
        <li>
            <xsl:if test="descendant-or-self::*[@id = $currentPage/@id]">
                <xsl:attribute name="class">current</xsl:attribute>
            </xsl:if>

            <xsl:if test="count(./* [@isDoc and string(umbracoNaviHide) != '1' and string(@template) != '0']) = 0">
                <a href="{umbraco.library:NiceUrl(@id)}">
                    <xsl:value-of select="@nodeName"/>
                </a>
            </xsl:if>

            <xsl:if test="count(./* [@isDoc and string(umbracoNaviHide) != '1' and string(@template) != '0']) > 0">
                <a href="{umbraco.library:NiceUrl(@id)}">
                    <xsl:value-of select="@nodeName"/>
                </a>
                <ul>
                    <xsl:choose>
                        <xsl:when test="./umbracoNaviHide != '1'">
                            <li>
                                <xsl:if test="descendant-or-self::*[@id = $currentPage/@id]">
                                    <xsl:attribute name="class">current</xsl:attribute>
                                </xsl:if>
                                <a href="{umbraco.library:NiceUrl(@id)}">
                                    <xsl:value-of select="@nodeName"/>
                                </a>
                            </li>
                        </xsl:when>
                    </xsl:choose>
                    <xsl:call-template name="drawNodes">
                        <xsl:with-param name="parent" select="."/>
                    </xsl:call-template>
                </ul>
            </xsl:if>
        </li>
    </xsl:for-each>
</xsl:template>

</xsl:stylesheet>

这是手动添加的嵌套列表 XSLT:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xsl:stylesheet [ <!ENTITY nbsp "&#x00A0;"> ]>
<xsl:stylesheet
  version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:msxml="urn:schemas-microsoft-com:xslt"
  xmlns:umbraco.library="urn:umbraco.library"
  exclude-result-prefixes="msxml umbraco.library">

<xsl:output method="xml" omit-xml-declaration="yes"/>

<xsl:param name="currentPage"/>

<xsl:template match="/">
<xsl:variable name="level1" select="$currentPage/ancestor-or-self::* [@level = 2 and string(umbracoNaviHide) != '1' and string(hideFromSideNavigation) != '1']" />  
<xsl:variable name="level" select="2"/>

<div class="subnav">
    <!-- LEVEL 1 -->
    <p><xsl:value-of select="$level1/@nodeName"/></p>
    <!-- LEVEL 2 -->
    <ul>
        <xsl:for-each select="$currentPage/ancestor-or-self::* [@level=$level]/* [@isDoc and string(umbracoNaviHide) != '1' and string(@template) != '0']">
            <li>
                <xsl:if test="descendant-or-self::*[@id = $currentPage/@id]">
                    <xsl:attribute name="class">current</xsl:attribute>
                </xsl:if>
                <a href="{umbraco.library:NiceUrl(@id)}">
                    <xsl:value-of select="@nodeName"/>
                </a>
                <!-- LEVEL 3 if any more nodes are Visible -->
                <xsl:if test="*[@isDoc][not(umbracoNaviHide = 1)]">
                    <ul>
                        <xsl:for-each select="./* [@isDoc and string(umbracoNaviHide) != '1' and string(@template) != '0']">
                            <li>
                                <xsl:if test="descendant-or-self::*[@id = $currentPage/@id]">
                                    <xsl:attribute name="class">current</xsl:attribute>
                                </xsl:if>
                                <a href="{umbraco.library:NiceUrl(@id)}">
                                    <xsl:value-of select="@nodeName"/>
                                </a>
                                <!-- LEVEL 4 if any more nodes are Visible -->
                                <xsl:if test="*[@isDoc][not(umbracoNaviHide = 1)]">
                                    <ul>
                                        <xsl:for-each select="./* [@isDoc and string(umbracoNaviHide) != '1' and string(@template) != '0']">
                                            <li>
                                                <xsl:if test="descendant-or-self::*[@id = $currentPage/@id]">
                                                    <xsl:attribute name="class">current</xsl:attribute>
                                                </xsl:if>
                                                <a href="{umbraco.library:NiceUrl(@id)}">
                                                    <xsl:value-of select="@nodeName"/>
                                                </a>
                                                <!-- LEVEL 5 if any more nodes are Visible -->
                                                <xsl:if test="*[@isDoc][not(umbracoNaviHide = 1)]">
                                                    <ul>
                                                        <xsl:for-each select="./* [@isDoc and string(umbracoNaviHide) != '1' and string(@template) != '0']">
                                                            <li>
                                                                <xsl:if test="descendant-or-self::*[@id = $currentPage/@id]">
                                                                    <xsl:attribute name="class">current</xsl:attribute>
                                                                </xsl:if>
                                                                <a href="{umbraco.library:NiceUrl(@id)}">
                                                                    <xsl:value-of select="@nodeName"/>
                                                                </a>
                                                            </li>
                                                        </xsl:for-each>
                                                    </ul>
                                                </xsl:if>
                                            </li>
                                        </xsl:for-each>
                                    </ul>
                                </xsl:if>
                            </li>
                        </xsl:for-each>
                    </ul>
                </xsl:if>
            </li>
        </xsl:for-each>
    </ul>
</div>
</xsl:template>

</xsl:stylesheet>

任何人都可以提出解决方案吗?

4

1 回答 1

0

在 Umbraco 论坛上发布相同的问题后,我收到了以下链接: http: //our.umbraco.org/projects/website-utilities/cogworks-flexible-navigation

它很完美,希望能帮助其他人。一个伟大的包,允许垂直和水平导航类型。

于 2012-09-19T03:53:01.520 回答