这种转变:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:param name="pNumCols" select="3"/>
<xsl:variable name="vCount" select="count(/*/*)"/>
<xsl:variable name="vRemainder"
select="$vCount mod $pNumCols"/>
<xsl:variable name="vPerCol" select="floor($vCount div $pNumCols)"/>
<xsl:template match="/*">
<table border="1">
<xsl:apply-templates select="*[not(position() > ceiling($vCount div $pNumCols))]"/>
</table>
</xsl:template>
<xsl:template match="link">
<xsl:variable name="vCurrentItem" select="."/>
<xsl:variable name="vRow" select="position()"/>
<tr>
<xsl:for-each select=
"(//node()|//namespace::*)[not(position() > $pNumCols)]">
<xsl:variable name="vColNo" select="position()"/>
<xsl:apply-templates mode="inGroup" select=
"$vCurrentItem/../*
[position() = ($vColNo -1)*$vPerCol+ $vRemainder
- ($vRemainder - $vColNo +1)*not($vColNo > $vRemainder)+ $vRow
]
[not($vRow > $vPerCol and $vColNo > $vRemainder)]"/>
</xsl:for-each>
</tr>
</xsl:template>
<xsl:template match="link" mode="inGroup">
<td><xsl:number count="link"/>. <xsl:copy-of select="text()"/></td>
</xsl:template>
</xsl:stylesheet>
应用于提供的 XML 文档时:
<links>
<link>one</link>
<link>two</link>
<link>three</link>
<link>four</link>
<link>five</link>
<link>six</link>
<link>seven</link>
<link>eight</link>
<link>nine</link>
<link>ten</link>
<link>eleven</link>
<link>twelve</link>
<link>thirteen</link>
</links>
产生想要的正确结果:
<table border="1">
<tr>
<td>1. one</td>
<td>6. six</td>
<td>10. ten</td>
</tr>
<tr>
<td>2. two</td>
<td>7. seven</td>
<td>11. eleven</td>
</tr>
<tr>
<td>3. three</td>
<td>8. eight</td>
<td>12. twelve</td>
</tr>
<tr>
<td>4. four</td>
<td>9. nine</td>
<td>13. thirteen</td>
</tr>
<tr>
<td>5. five</td>
</tr>
</table>
如果我们指定$pNumCols
为 4,则转换再次产生正确的结果:
<table border="1">
<tr>
<td>1. one</td>
<td>5. five</td>
<td>8. eight</td>
<td>11. eleven</td>
</tr>
<tr>
<td>2. two</td>
<td>6. six</td>
<td>9. nine</td>
<td>12. twelve</td>
</tr>
<tr>
<td>3. three</td>
<td>7. seven</td>
<td>10. ten</td>
<td>13. thirteen</td>
</tr>
<tr>
<td>4. four</td>
</tr>
</table>