【发布时间】:2016-07-18 07:43:38
【问题描述】:
我有一个这样的 XML 文档
<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 中得到一个这样的结果
<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>
基本上我不确定如何将每个 2 级项目包装在 div 和 ul 元素中,我也在寻找一种递归方法,以便它也可以处理 2 级以下的 3 级。任何帮助都会很棒!
【问题讨论】: