0
我有這樣XSLT導航應用模板子節點
<Page ID="28" AreaID="1" MenuText="Om IAI" Href="Default.aspx?ID=28" FriendlyHref="/da-dk/om-iai.aspx" Allowclick="True" Hidden="False" ShowInSitemap="True" ShowInLegend="True" AbsoluteLevel="1" RelativeLevel="1" Sort="1" LastInLevel="False" InPath="True" ChildCount="2" class="L1_Active" Active="True" IsPagePasswordProtected="False" IsPageUserProtected="False" CanAccessPasswordProtectedPage="False" CanAccessUserProtectedPage="True">
<Page ID="29" AreaID="1" MenuText="Underside med langt navn" Href="Default.aspx?ID=29" FriendlyHref="/da-dk/om-iai/underside-med-langt-navn.aspx" Allowclick="True" Hidden="False" ShowInSitemap="True" ShowInLegend="True" AbsoluteLevel="2" RelativeLevel="2" Sort="1" LastInLevel="False" InPath="False" ChildCount="0" class="L2" Active="False" IsPagePasswordProtected="False" IsPageUserProtected="False" CanAccessPasswordProtectedPage="False" CanAccessUserProtectedPage="True" />
<Page ID="30" AreaID="1" MenuText="SubPage 2" Href="Default.aspx?ID=30" FriendlyHref="/da-dk/om-iai/subpage-2.aspx" Allowclick="True" Hidden="False" ShowInSitemap="True" ShowInLegend="True" AbsoluteLevel="2" RelativeLevel="2" Sort="2" LastInLevel="True" InPath="False" ChildCount="0" class="L2" Active="False" IsPagePasswordProtected="False" IsPageUserProtected="False" CanAccessPasswordProtectedPage="False" CanAccessUserProtectedPage="True" />
</Page>
一個XML文檔,我想出來這樣從上面的XML
<ul>
<li>
<a>level 1</a>
<nav>
<div><ul><li><a>level 2</a></li></ul></div>
<div><ul><li><a>level 2</a></li></ul></div>
</nav>
</li>
</ul>
我的XSLT模板看起來像這樣
<xsl:template match="//Page">
<xsl:param name="depth"/>
<xsl:copy-of select="."/>
<li>
<xsl:attribute name="class">
<xsl:if test="@ChildCount > 0">dropdown </xsl:if>
</xsl:attribute>
<xsl:choose>
<xsl:when test="@ChildCount > 0">
<a>
<xsl:attribute name="href">
<xsl:value-of select="@FriendlyHref" disable-output-escaping="yes"/>
</xsl:attribute>
<xsl:attribute name="class">
<xsl:text>dropdown-toggle</xsl:text>
</xsl:attribute>
<xsl:attribute name="data-toggle">
<xsl:text>dropdown</xsl:text>
</xsl:attribute>
<xsl:value-of select="@MenuText" disable-output-escaping="yes"/>
</a>
</xsl:when>
<xsl:otherwise>
<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:otherwise>
</xsl:choose>
<xsl:if test="count(Page)">
<nav class="dropdown-menu col-sm-12">
<xsl:call-template name="MegaMenu">
<xsl:with-param name="depth" select="$depth+1"/>
</xsl:call-template>
</nav>
</xsl:if>
</li>
</xsl:template>
基本上我不確定如何在div和ul元素中包裝每個級別2項目,我也在尋找遞歸e方法,所以它也可以處理2級以下的級別3.任何幫助都會很棒!
昨天我解決我的問題,正好你提出同樣的方式:) –