Xml 如何在XSL中将一个表垂直拆分为两个表?
如果我有下表:Xml 如何在XSL中将一个表垂直拆分为两个表?,xml,xslt,position,split,Xml,Xslt,Position,Split,如果我有下表: <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<
<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>
123456
ABCDEF
我将如何在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>
123
基础知识
456
DEF
我对一种通用方法感兴趣,在这种方法中,表可以有任何维度,并且可以拆分为两个以上的表。我不在乎争吵;我想在超过N列的情况下进行拆分,最后得到TD/N表,其中TD是一个表数据单元。例如,如果有12列25行,我想要4个表,每个表有3列25行。试试这个。这应该可以在XSLT1.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>
3.
以下是我的看法:
<xsl:stylesheet
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
>
<xsl:variable name="split" select="3" />
<xsl:template match="table">
<xsl:variable name="self" select="." />
<!-- select <td>1</td>, <td>4</td> -->
<xsl:for-each select="tr[1]/td[position() mod $split = 1]">
<xsl:apply-templates select="$self" mode="split">
<!-- calculate & pass the starting position for copying <td>s -->
<xsl:with-param name="start" select="$split * (position() - 1)" />
</xsl:apply-templates>
</xsl:for-each>
</xsl:template>
<!-- this just copies the table an passes on $start -->
<xsl:template match="table" mode="split">
<xsl:param name="start" select="0" />
<xsl:copy>
<xsl:apply-templates select="tr" mode="split">
<xsl:with-param name="start" select="$start" />
</xsl:apply-templates>
</xsl:copy>
</xsl:template>
<!-- this copies <tr>/<td> based on $start -->
<xsl:template match="tr" mode="split">
<xsl:param name="start" select="0" />
<xsl:copy>
<xsl:copy-of select="td[
position() > $start and position() <= $start + $split
]" />
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
结果:
<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>
123
基础知识
456
DEF