将节点列表拆分为多个部分
xml:
<mode>1</mode>
<mode>2</mode>
<mode>3</mode>
<mode>4</mode>
<mode>5</mode>
<mode>6</mode>
<mode>7</mode>
<mode>8</mode>
<mode>9</mode>
<mode>10</mode>
<mode>11</mode>
<mode>12</mode>
我需要将其分开(例如,4):
xslt:
<xsl:variable name="vNodes" select="mode"/>
<xsl:variable name="vNumParts" select="4"/>
<xsl:variable name="vNumCols" select="ceiling(count($vNodes) div $vNumParts)"/>
<xsl:for-each select="$vNodes[position() mod $vNumCols = 1]">
<xsl:variable name="vCurPos" select="(position()-1)*$vNumCols +1"/>
<ul>
<xsl:for-each select="$vNodes[position() >= $vCurPos and not(position() > $vCurPos + $vNumCols -1)]">
<li><xsl:value-of select="."/></li>
</xsl:for-each>
</ul>
</xsl:for-each>
此代码是由 Dimitre Novatchev 编写的 - 伟大的编码器)),
但节点数量少于部分数量(例如,我有 2 < code>modes)此代码不起作用 - 它不输出任何内容。
在这种情况下它如何升级(没有选择
构造)?
xml:
<mode>1</mode>
<mode>2</mode>
<mode>3</mode>
<mode>4</mode>
<mode>5</mode>
<mode>6</mode>
<mode>7</mode>
<mode>8</mode>
<mode>9</mode>
<mode>10</mode>
<mode>11</mode>
<mode>12</mode>
i need to separate it on parts (for ex. on 4):
xslt:
<xsl:variable name="vNodes" select="mode"/>
<xsl:variable name="vNumParts" select="4"/>
<xsl:variable name="vNumCols" select="ceiling(count($vNodes) div $vNumParts)"/>
<xsl:for-each select="$vNodes[position() mod $vNumCols = 1]">
<xsl:variable name="vCurPos" select="(position()-1)*$vNumCols +1"/>
<ul>
<xsl:for-each select="$vNodes[position() >= $vCurPos and not(position() > $vCurPos + $vNumCols -1)]">
<li><xsl:value-of select="."/></li>
</xsl:for-each>
</ul>
</xsl:for-each>
this code is written by Dimitre Novatchev - great coder))
but for the number of nodes less then number of parts (for ex. i have 2 modes
) this code does not work - it outputs nothing.
How it upgrade for that case (without choose
construction)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尽管如果节点数量小于零件数量,则问题定义不正确,这是一个我猜测产生OP最可能想要的输出的转换(为什么他不是刚刚指定了此行为吗???):
当此转换应用于以下 XML 文档时(他甚至无法提供格式良好的 XML 文档!):
输出是我猜测OP想要的......
Although the problem is incorrectly defined if the number of nodes is smaller than the number of parts, here is a transformation that I guess produces the output the OP most probably wants (Why didn't he just specify this behavior???):
when this transformation is applied on the following XML document (he can't even provide a well-formed XML document!):
the output is what I guess the OP wanted...
实际上,代码可以正常工作。
每当部分数量大于节点数量时,就无法解决以下问题:“将 2 个节点分成数量相等的 4 个部分”——唯一的解决方案是每个部分包含 0节点。
现在您正在解决一个新的、不同的问题,难怪另一个不同问题的解决方案不适用于这个新问题。
正确的方法是正确地提出新问题并提出它。那么很多人就会很乐意回答。
Actually, the code works correctly.
Whenever the number of parts is greater than the number of nodes, there is no solution to the problem: "Divide 2 nodes into 4 parts in equal number" -- the only solution is that each part contains 0 nodes.
Now you are solving a new, different problem and no wonder the solution to a different problem does not work for this new problem.
The way to go is to formulate the new problem correctly and to ask it. Then many people will be glad to answer.