我有一个基于xslt的导航,并且它输出了以下XML
<Page ID="19" AreaID="1" MenuText="Language Selector" Href="Default.aspx?ID=19" FriendlyHref="/da-dk/globalelements/toolsnavigation/language-selector.aspx" Allowclick="True" Hidden="False" ShowInSitemap="True" ShowInLegend="True" AbsoluteLevel="3" RelativeLevel="3" Sort="1" LastInLevel="False" InPath="False" ChildCount="2" class="L3" Active="False" IsPagePasswordProtected="False" IsPageUserProtected="False" CanAccessPasswordProtectedPage="False" CanAccessUserProtectedPage="True">
<Page ID="20" AreaID="1" MenuText="DK" Href="Default.aspx?ID=20" FriendlyHref="/da-dk/globalelements/toolsnavigation/language-selector/dk.aspx" Allowclick="True" Hidden="False" ShowInSitemap="True" ShowInLegend="True" AbsoluteLevel="4" RelativeLevel="4" Sort="1" LastInLevel="False" InPath="False" ChildCount="0" class="L4" Active="False" IsPagePasswordProtected="False" IsPageUserProtected="False" CanAccessPasswordProtectedPage="False" CanAccessUserProtectedPage="True" />
<Page ID="21" AreaID="1" MenuText="NO" Href="Default.aspx?ID=21" FriendlyHref="/da-dk/globalelements/toolsnavigation/language-selector/no.aspx" Allowclick="True" Hidden="False" ShowInSitemap="True" ShowInLegend="True" AbsoluteLevel="4" RelativeLevel="4" Sort="2" LastInLevel="True" InPath="False" ChildCount="0" class="L4" Active="False" IsPagePasswordProtected="False" IsPageUserProtected="False" CanAccessPasswordProtectedPage="False" CanAccessUserProtectedPage="True" />
</Page>
<Page ID="27" AreaID="1" MenuText="Quicklinks" Href="Default.aspx?ID=27" FriendlyHref="/da-dk/globalelements/toolsnavigation/quicklinks.aspx" Allowclick="True" Hidden="False" ShowInSitemap="True" ShowInLegend="True" AbsoluteLevel="3" RelativeLevel="3" Sort="2" LastInLevel="True" InPath="False" ChildCount="2" class="L3" Active="False" IsPagePasswordProtected="False" IsPageUserProtected="False" CanAccessPasswordProtectedPage="False" CanAccessUserProtectedPage="True">
<Page ID="17" AreaID="1" MenuText="Tool 1" Href="Default.aspx?ID=17" FriendlyHref="/da-dk/globalelements/toolsnavigation/quicklinks/tool-1.aspx" Allowclick="True" Hidden="False" ShowInSitemap="True" ShowInLegend="True" AbsoluteLevel="4" RelativeLevel="4" Sort="1" LastInLevel="False" InPath="False" ChildCount="0" class="L4" Active="False" IsPagePasswordProtected="False" IsPageUserProtected="False" CanAccessPasswordProtectedPage="False" CanAccessUserProtectedPage="True" />
<Page ID="18" AreaID="1" MenuText="Tool 2" Href="Default.aspx?ID=18" FriendlyHref="/da-dk/globalelements/toolsnavigation/quicklinks/tool-2.aspx" Allowclick="True" Hidden="False" ShowInSitemap="True" ShowInLegend="True" AbsoluteLevel="4" RelativeLevel="4" Sort="2" LastInLevel="True" InPath="False" ChildCount="0" class="L4" Active="False" IsPagePasswordProtected="False" IsPageUserProtected="False" CanAccessPasswordProtectedPage="False" CanAccessUserProtectedPage="True" />
</Page>
渴望从这应该是这样的
<ul>
<li><a>DK</a></li>
<li><a>NO</a><li>
</ul>
<ul>
<li><a>Tool 1</a><li>
<li><a>Tool 2</a><li>
</ul>
以下是我到目前为止尝试过的XSLT代码
<xsl:template match="/NavigationTree">
<xsl:if test="count(//Page) > 0">
<div class="container">
<xsl:apply-templates select="Page">
<xsl:with-param name="depth" select="1"/>
</xsl:apply-templates>
</div>
</xsl:if>
</xsl:template>
<xsl:template match="//Page">
<xsl:param name="depth"/>
<li>
<a>
<xsl:attribute name="href">
<xsl:value-of select="@FriendlyHref" disable-output-escaping="yes"/>
</xsl:attribute>
<xsl:value-of select="@MenuText" disable-output-escaping="yes"/>
</a>
<xsl:if test="count(Page)">
<ul>
<xsl:apply-templates select="Page">
<xsl:with-param name="depth" select="$depth+1"/>
</xsl:apply-templates>
</ul>
</xsl:if>
</li>
</xsl:template>
请提前帮助
答案 0 :(得分:-1)
如果我改变你的第一个模板:
<xsl:template match="/NavigationTree">
<xsl:if test="count(//Page) > 0">
<div class="container">
<ul>
<xsl:apply-templates select="Page">
<xsl:with-param name="depth" select="1" />
</xsl:apply-templates>
</ul>
</div>
</xsl:if>
</xsl:template>
并用
包围你的xml<Navigation>...</Navigation>
然后我得到
<?xml version="1.0" encoding="UTF-8"?>
<div class="container">
<ul>
<li>
<a href="/da-dk/globalelements/toolsnavigation/language-selector.aspx">Language Selector</a>
<ul>
<li>
<a href="/da-dk/globalelements/toolsnavigation/language-selector/dk.aspx">DK</a>
</li>
<li>
<a href="/da-dk/globalelements/toolsnavigation/language-selector/no.aspx">NO</a>
</li>
</ul>
</li>
<li>
<a href="/da-dk/globalelements/toolsnavigation/quicklinks.aspx">Quicklinks</a>
<ul>
<li>
<a href="/da-dk/globalelements/toolsnavigation/quicklinks/tool-1.aspx">Tool 1</a>
</li>
<li>
<a href="/da-dk/globalelements/toolsnavigation/quicklinks/tool-2.aspx">Tool 2</a>
</li>
</ul>
</li>
</ul>
</div>
这是你喜欢的,不是吗?
答案 1 :(得分:-1)
在您的网页模板中,您可以使用<xsl:choose>
来指定depth
具有特定值时会发生什么:
<xsl:template match="/NavigationTree">
<xsl:if test="count(//Page) > 0">
<div class="container">
<xsl:apply-templates select="Page">
<xsl:with-param name="depth" select="1"/>
</xsl:apply-templates>
</div>
</xsl:if>
</xsl:template>
<xsl:template match="//Page">
<xsl:param name="depth"/>
<xsl:choose>
<xsl:when test="$depth = 1 and count(Page)">
<ul>
<xsl:apply-templates select="Page">
<xsl:with-param name="depth" select="$depth+1"/>
</xsl:apply-templates>
</ul>
</xsl:when>
<xsl:otherwise>
<li>
<a>
<xsl:attribute name="href">
<xsl:value-of select="@FriendlyHref" disable-output-escaping="yes"/>
</xsl:attribute>
<xsl:value-of select="@MenuText" disable-output-escaping="yes"/>
</a>
</li>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
这应该产生所需的输出。