XSL:将xml转换为排序的多列html表
我正在研究一个类似于本文所述的问题 但是有一点皱纹。我的XML没有任何特定的顺序,我想对其进行排序以显示。例如,我的XML是XSL:将xml转换为排序的多列html表,xml,xslt,Xml,Xslt,我正在研究一个类似于本文所述的问题 但是有一点皱纹。我的XML没有任何特定的顺序,我想对其进行排序以显示。例如,我的XML是 <items> <item>A</item> <item>C</item> <item>E</item> <item>B</item> <item>D</item> <!-- ... an
<items>
<item>A</item>
<item>C</item>
<item>E</item>
<item>B</item>
<item>D</item>
<!-- ... any number of item nodes ... -->
<item>
A.
C
E
B
D
我希望我的输出是(出于说明目的,我忽略了未命名的节点)
A.
C
E
B
D
我所基于的XSL来自上面的链接(我需要使用XSL 1.0):
我曾尝试在各种应用模板中插入命令,但这不起作用
想法
杰夫
根据评论更新
我想输出一个多色表
在条目所在的3列中
按字母顺序垂直排列
更新:现在,在解释了新的需求之后,此样式表:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt"
exclude-result-prefixes="msxsl">
<xsl:strip-space elements="*"/>
<xsl:param name="pColumns" select="3"/>
<xsl:template match="items">
<xsl:variable name="vrtfChilds">
<xsl:for-each select="*">
<xsl:sort/>
<xsl:copy-of select="."/>
</xsl:for-each>
</xsl:variable>
<xsl:variable name="vChilds" select="msxsl:node-set($vrtfChilds)/*"/>
<xsl:variable name="vRows"
select="ceiling(count($vChilds) div $pColumns)"/>
<table>
<xsl:for-each select="$vChilds[$vRows >= position()]">
<tr>
<xsl:call-template name="columns">
<xsl:with-param name="pRows" select="$vRows"/>
</xsl:call-template>
</tr>
</xsl:for-each>
</table>
</xsl:template>
<xsl:template name="columns">
<xsl:param name="pData" select="."/>
<xsl:param name="pColumn" select="$pColumns"/>
<xsl:param name="pRows" select="0"/>
<xsl:if test="$pColumn">
<td>
<xsl:apply-templates select="$pData"/>
</td>
<xsl:call-template name="columns">
<xsl:with-param name="pData"
select="$pData/following-sibling::*[$pRows]"/>
<xsl:with-param name="pColumn" select="$pColumn - 1"/>
<xsl:with-param name="pRows" select="$pRows"/>
</xsl:call-template>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
输出:
<table>
<tr>
<td>A</td>
<td>C</td>
<td>E</td>
</tr>
<tr>
<td>B</td>
<td>D</td>
<td></td>
</tr>
</table>
A.
C
E
B
D
注意:
节点集
两相转换的扩展功能。此转换:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ext="http://exslt.org/common"
exclude-result-prefixes="ext">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:param name="pNumCols" select="3"/>
<xsl:variable name="vNumRows" select=
"ceiling(count(/*/*) div $pNumCols)"/>
<xsl:variable name="vrtfSorted">
<xsl:for-each select="/*/*">
<xsl:sort/>
<xsl:copy-of select="."/>
</xsl:for-each>
</xsl:variable>
<xsl:variable name="vSorted"
select="ext:node-set($vrtfSorted)/*"/>
<xsl:template match="/">
<table>
<xsl:apply-templates select=
"$vSorted[not(position() > $vNumRows)]"/>
</table>
</xsl:template>
<xsl:template match="item">
<tr>
<xsl:apply-templates select=
"(.|following-sibling::*[position() mod $vNumRows =0])/text()"/>
</tr>
</xsl:template>
<xsl:template match="text()">
<td><xsl:value-of select="."/></td>
</xsl:template>
</xsl:stylesheet>
应用于提供的XML文档时:
<items>
<item>A</item>
<item>C</item>
<item>E</item>
<item>B</item>
<item>D</item>
</items>
<table>
<tr>
<td>A</td>
<td>C</td>
<td>E</td>
</tr>
<tr>
<td>B</td>
<td>D</td>
</tr>
</table>
A.
C
E
B
D
生成所需的正确结果:
<items>
<item>A</item>
<item>C</item>
<item>E</item>
<item>B</item>
<item>D</item>
</items>
<table>
<tr>
<td>A</td>
<td>C</td>
<td>E</td>
</tr>
<tr>
<td>B</td>
<td>D</td>
</tr>
</table>
A.
C
E
B
D
谢谢,但这不太管用。然而,对于我作为示例指定的XML顺序,它工作得很好。但是,比方说,我在XML中的任意位置添加了一个项F。XSL不会按照我需要的顺序对它进行排序(第一行:ace,第二行:bdf)。@Jeffrey Hersh:您写了“这不太管用”和“它很管用”。我想你需要下定决心。。。如果有一些分类涉及,你应该在你的问题中描述。对不起,我不清楚。您的解决方案对于我提供的示例很好,但在一般情况下不起作用。一般情况是我需要的。对不起。。。具体来说,我有一个长xml文件,其中包含各种节点和各种子字段。我想对这些子字段中的一个进行排序,以便在指定的子字段上按字母顺序生成一个多列表。因此,在这个示例中,我发布的xml可以有任意数量的条目,并且这些条目可以是任意顺序的。我想输出一个有3列的多色表,其中条目按字母顺序垂直排列。这就是我所说的一般情况。@Jeffrey Hersh:至少,你应该接受两个答案中的一个——Alejandro和我都提供了正确和良好的答案。要接受答案,请单击答案旁边的绿色复选标记。