XSL:将 xml 转换为排序的多列 html 表
我正在看一个类似于此处涵盖的问题
但有轻微的皱纹。我的XML没有任何特定顺序,我想对其进行排序以显示。例如,我的XML是
<items>
<item>A</item>
<item>C</item>
<item>E</item>
<item>B</item>
<item>D</item>
<!-- ... any number of item nodes ... -->
<item>
,我希望我的输出是(出于说明目的而忽略了非命名节点的地方)
<table>
<tr>
<td>A</td>
<td>C</td>
<td>E</td>
</tr>
<tr>
<td>B</td>
<td>D</td>
<td />
</tr>
</table>
XSL我依据的XSL来自上述链接(我需要使用XSL 1.0):
<xsl:template match="/*">
<table>
<xsl:call-template name="make-columns">
<xsl:with-param name="nodelist" select="item"/>
</xsl:call-template>
</table>
</xsl:template>
<xsl:template name="make-columns">
<xsl:param name="nodelist"/>
<xsl:param name="columns-number" select="3"/>
<tr>
<xsl:apply-templates select="$nodelist[
not(position() > $columns-number)
]"/>
<xsl:if test="count($nodelist) < $columns-number">
<xsl:call-template name="empty-cells">
<xsl:with-param name="finish"
select="$columns-number - count($nodelist)"/>
</xsl:call-template>
</xsl:if>
</tr>
<!-- If some nodes are left, recursively call current
template, passing only nodes that are left -->
<xsl:if test="count($nodelist) > $columns-number">
<xsl:call-template name="make-columns">
<xsl:with-param name="nodelist" select="$nodelist[
position() > $columns-number
]"/>
</xsl:call-template>
</xsl:if>
</xsl:template>
<xsl:template match="item">
<td>
<xsl:apply-templates/>
</td>
</xsl:template>
<xsl:template name="empty-cells">
<xsl:param name="finish"/>
<td/>
<xsl:if test="not($finish = 1)">
<xsl:call-template name="empty-cells">
<xsl:with-param name="finish" select="$finish - 1"/>
</xsl:call-template>
</xsl:if>
</xsl:template>
我尝试在各种应用程序中插入命令,但这无效。
想法?
杰夫
评论更新
我想输出一个多列表 有 3 列,其中条目是 按字母顺序垂直
I am looking at a similar problem to what was covered here
Transforming List into a 2-D Table
but with a slight wrinkle. My XML is not in any particular order and I would like to sort it for display. For example my XML is
<items>
<item>A</item>
<item>C</item>
<item>E</item>
<item>B</item>
<item>D</item>
<!-- ... any number of item nodes ... -->
<item>
and I want my output to be (where I am ignoring the non-named nodes for illustrative purposes)
<table>
<tr>
<td>A</td>
<td>C</td>
<td>E</td>
</tr>
<tr>
<td>B</td>
<td>D</td>
<td />
</tr>
</table>
The XSL I am basing this off is from the above link (I need to use XSL 1.0):
<xsl:template match="/*">
<table>
<xsl:call-template name="make-columns">
<xsl:with-param name="nodelist" select="item"/>
</xsl:call-template>
</table>
</xsl:template>
<xsl:template name="make-columns">
<xsl:param name="nodelist"/>
<xsl:param name="columns-number" select="3"/>
<tr>
<xsl:apply-templates select="$nodelist[
not(position() > $columns-number)
]"/>
<xsl:if test="count($nodelist) < $columns-number">
<xsl:call-template name="empty-cells">
<xsl:with-param name="finish"
select="$columns-number - count($nodelist)"/>
</xsl:call-template>
</xsl:if>
</tr>
<!-- If some nodes are left, recursively call current
template, passing only nodes that are left -->
<xsl:if test="count($nodelist) > $columns-number">
<xsl:call-template name="make-columns">
<xsl:with-param name="nodelist" select="$nodelist[
position() > $columns-number
]"/>
</xsl:call-template>
</xsl:if>
</xsl:template>
<xsl:template match="item">
<td>
<xsl:apply-templates/>
</td>
</xsl:template>
<xsl:template name="empty-cells">
<xsl:param name="finish"/>
<td/>
<xsl:if test="not($finish = 1)">
<xsl:call-template name="empty-cells">
<xsl:with-param name="finish" select="$finish - 1"/>
</xsl:call-template>
</xsl:if>
</xsl:template>
I have tried inserting commands within the various apply-templates but that doesn't work.
ideas?
Jeff
Update from comments
I want to output a multicolum table
with 3 columns where the entries are
in alphabetical order vertically
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
此转换:
应用于提供的 XML 文档时:
产生所需的正确结果:
This transformation:
when applied on the provided XML document:
produces the wanted, correct result:
更新:现在,解释了新的要求,此样式表:
输出:
注意:用于两阶段转换的
node-set
扩展函数。Update: Now, with new requeriment explained, this stylesheet:
Output:
Note:
node-set
extension function for a two phase transformation.