我有一个xml:
<sitemapnode>
<node id="1"/>
<node id="2"/>
<node id="3"/>
<node id="4"/>
<node id="5"/>
<node id="6"/>
<node id="7"/>
<node id="8"/>
</sitemapnode>
我想把它转换成一个看起来像这样的html;
<div>
<ul>
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
</ul>
</div>
<div>
<ul>
<li>5</li>
<li>6</li>
<li>7</li>
<li>8</li>
</ul>
</div>
如何遍历所有节点并分别为它们的每一半提供 div 和 uls?此外,如果节点数为奇数,则可以在前半部分节点数减半后进行上限。自 3 天以来,我一直在努力解决这个问题。并且还没有找到任何解决方案。求求求帮助...
这是实际的代码:
<xsl:template match="/">
<div>
<!-- The menu starts here. -->
<ul class="nav" id="main-nav">
<!-- Looping the sitemap nodes. -->
<xsl:for-each select="siteMap/siteMapNode/siteMapNode">
<!-- checking if the node is visible on top nav or not. -->
<xsl:if test="@isVisibleInTopnav = 'true'">
<!-- Creating the L1 navigation. -->
<li class="drop">
<a class="top_link">
<xsl:attribute name="href">
<xsl:value-of select="@url"/>
</xsl:attribute>
<span>
<xsl:value-of select="@topNavTitle"/>
</span>
</a>
<!-- Creating the L2 navigation, L3 navigations..... -->
<div class="nav-sub">
<div class="inner">
<xsl:call-template name="nested_levels">
</xsl:call-template>
</div>
</div>
</li>
</xsl:if>
</xsl:for-each>
</ul>
</div>
</xsl:template>
<xsl:template name="nested_levels">
<xsl:param name="nodeCount" select="count(siteMapNode)"/>
<xsl:variable name="columns" select="ceiling($nodeCount div 2)"/>
<xsl:if test="count(siteMapNode) > 0">
<xsl:for-each select="siteMapNode">
<xsl:if test="ceiling((position() - 1) div $columns) = (position() - 1) div $columns">
<div class="row mod">
<xsl:apply-templates select="self::node|following-sibling::node[position() < $columns]" mode="list" />
</div>
</xsl:if>
</xsl:for-each>
</xsl:if>
</xsl:template>
<xsl:template match="node" mode="list">
<div> <span class="h1">
<a>
<xsl:attribute name="href">
<xsl:value-of select="@url"/>
</xsl:attribute>
<xsl:value-of select="@topNavTitle"/>
</a>
</span>
</div>
</xsl:template>
这是示例 xml:
<?xml version="1.0" encoding="utf-8"?>
<siteMap>
<siteMapNode url="/" isVisibleInTopnav="" isVisibleInRightnav="" isVisibleInSitemap="" topNavTitle="" rightNavTitle="" hasBlueBackground="" showChildNav="" locatorKey="">
<siteMapNode url="/a/" isVisibleInTopnav="true" isVisibleInRightnav="false" isVisibleInSitemap="true" topNavTitle="Personal Banking" rightNavTitle="" hasBlueBackground="false" showChildNav="false" locatorKey="">
<siteMapNode url="/a/b/" isVisibleInTopnav="true" isVisibleInRightnav="true" isVisibleInSitemap="true" topNavTitle="Checking" rightNavTitle="Checking Accounts" hasBlueBackground="false" showChildNav="false" locatorKey="">
<siteMapNode url="/a/b/c/" isVisibleInTopnav="true" isVisibleInRightnav="true" isVisibleInSitemap="false" topNavTitle="Checking Accounts" rightNavTitle="Compare Checking Accounts" hasBlueBackground="false" showChildNav="false" locatorKey="" />
</siteMapNode>
</siteMapNode>
</siteMapNode>
</siteMap>