如果我有下表:
<table>
<tr><td>1</td><td>2</td><td>3</td><td>4</td><td>5</td><td>6</td></tr>
<tr><td>A</td><td>B</td><td>C</td><td>D</td><td>E</td><td>F</td></tr>
</table>
我将如何在xslt中拆分它,以便得到以下结果:
<table>
<tr><td>1</td><td>2</td><td>3</td></tr>
<tr><td>A</td><td>B</td><td>C</td></tr>
</table>
<table>
<tr><td>4</td><td>5</td><td>6</td></tr>
<tr><td>D</td><td>E</td><td>F</td></tr>
</table>
我对一种广义的方法感兴趣,在这种方法中,表可以有任何维度,并且可以分成两个以上的表。我不关心行;我想在超过n列的地方拆分,最后得到td/n表,其中td是一个表数据单元。例如,如果有12列25行,我想要4个表,每个表有3列25行。
最佳答案
试试这个。这应该在xslt 1.0中有效。调整items变量以更改每个表所需的列数。
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="html" omit-xml-declaration="yes"/>
<xsl:variable name="ITEMS">3</xsl:variable>
<xsl:template match="//table">
<!-- Loop through the items in the first row -->
<xsl:for-each select="tr[position() = 1]/td">
<!-- Check if this item needs to be the start of a new row in a new table -->
<xsl:if test="position() mod $ITEMS = 1">
<!-- Get the current position which is used to get items from subsequent rows -->
<xsl:variable name="COLUMNNUMBER" select="position()"/>
<table>
<!-- Loop through all the rows in the table -->
<xsl:for-each select="../../tr">
<tr>
<!-- Output items within the required range using previously saved column number -->
<xsl:for-each select="td[position() >= $COLUMNNUMBER and position() < $COLUMNNUMBER + $ITEMS]">
<xsl:copy-of select="."/>
</xsl:for-each>
</tr>
</xsl:for-each>
</table>
</xsl:if>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>