如何在XSL中将一个表垂直拆分为两个表? [英] How can I split a table vertically into two tables in XSL?
问题描述
如果我有下表:
<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中拆分它,以便得到以下结果:
How would I split this in XSLT so that I end up with the following:
<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是表数据单元格的TD/N表结束.例如,如果有12列和25行,我想要4个表,每个表有3列和25行.
I am interested in a generalized method, where the table could have any dimensions and be split into more than two tables. I don't care about rows; I want to split where there are more than N columns and end up with TD/N tables where TD is a table data cell. E.g., if there are 12 columns and 25 rows, I'd like 4 tables, each with 3 columns and 25 rows.
推荐答案
尝试一下.这应该在XSLT 1.0中起作用.调整ITEMS变量,以更改每个表所需的列数.
Try this. This should work in XSLT 1.0 . Adjust the ITEMS variable to vary the number of columns you want for each table.
<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>
这篇关于如何在XSL中将一个表垂直拆分为两个表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!