对于某种输出格式(不是HTML格式),我需要将HTML表格转换为"方形"表格,其中每个colspan
和rowspan
不仅在父单元格中指示,而且还后跟正确数量的空单元格。
例如,简单的 HTML 表格
<table>
<tr>
<th>test</th>
<th colspan="2">span 1/2</th>
<th colspan="3">span 2/2</th>
</tr>
<tr>
<td>col 1</td>
<td>col 2</td>
<td>col 3</td>
<td>col 4</td>
<td>col 5</td>
<td>col 6</td>
</tr>
</table>
应翻译成
<table>
<tr>
<th>test</th>
<th colspan="2">span 1/2</th>
<th /> <!-- < empty cell added -->
<th colspan="3">span 2/2</th>
<th /> <!-- < empty cell added -->
</tr>
..
(注意:输出格式使用非常不同的语法,这只是为了清楚起见!
同样,行跨度应传播到接下来<tr>
行:
<table><tr><td rowspan="3" /><td rowspan="2" /><td /></tr>
<tr><td>data</td></tr>
<tr><td>data</td><td>data</td></tr>
</table>
应该出来作为
<table>
<tr><td /><td /><td /></tr>
<tr><td /><td /><td>data</td></tr> <!-- 2 empty cells added -->
<tr><td /><td>data</td><td>data</td></tr> <!-- 1 empty cell added -->
<table>
处理colspan
很简单:
<xsl:template name="add-empty">
<xsl:param name="repeat" />
<xsl:if test="$repeat > 1">
<td class="empty" />
<xsl:call-template name="add-empty">
<xsl:with-param name="repeat" select="$repeat - 1" />
</xsl:call-template>
</xsl:if>
</xsl:template>
<xsl:template match="th|td">
<td>
<xsl:apply-templates />
</td>
<xsl:if test="@colspan">
<xsl:call-template name="add-empty">
<xsl:with-param name="repeat" select="@colspan" />
</xsl:call-template>
</xsl:if>
</xsl:template>
这将添加单个th
或td
,检查每个colspan
,并根据需要插入任意数量的空单元格,递归调用模板add-empty
。类属性empty
仅用于调试。
问题出在rowspan
。为了使它正常工作,它需要扫描每个以前的tr
,并记录哪些列需要为空。该迭代将类似于
<xsl:if test="position() > 1">
<xsl:variable name="currentRow" select="position()" />
<xsl:for-each select="../tr[position() < $currentRow]">
<xsl:message>testing <xsl:value-of select="." /></xsl:message>
</xsl:for-each>
</xsl:if>
– 它不需要在第一行调用,因为为此只需要添加colspan
。那么,问题是双重的:我将如何构建单元格集列表以加起来为当前行的正确集?有了这样的列表,我如何迭代这个列表(与表中的列总数一样长)和每一行的th|td
元素?
后者是一个问题,因为我可以使用类似的东西迭代单元格集
<xsl:for-each select="1 to string-length(cell-set)">
<xsl:if test="substring($cell-set, ., 1) = 'E'>
.. empty ..
...
</xsl:for-each>
(如果cell-set
是字符串),或者在"当前"tr
内容上使用
<xsl:for-each select="th|td">
..
在这种情况下,与cell-set
的内容没有直接关系。对于第一个,我不知道要插入哪个索引td|th
,第二个我不知道何时插入空白。
基于我已经在链接中提到的 http://andrewjwelch.com/code/xslt/table/table-normalization.html 您可以使用:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" version="2.0" exclude-result-prefixes="xs">
<xsl:output indent="yes" omit-xml-declaration="yes" />
<xsl:variable name="table_with_no_colspans">
<xsl:apply-templates mode="colspan" />
</xsl:variable>
<xsl:variable name="table_with_no_rowspans">
<xsl:for-each select="$table_with_no_colspans">
<xsl:apply-templates mode="rowspan" />
</xsl:for-each>
</xsl:variable>
<xsl:template match="/">
<xsl:apply-templates select="$table_with_no_rowspans" mode="final" />
</xsl:template>
<xsl:template match="@*|*" mode="#all">
<xsl:copy>
<xsl:apply-templates select="@*|*" mode="#current" />
</xsl:copy>
</xsl:template>
<xsl:template match="td | th" mode="colspan">
<xsl:choose>
<xsl:when test="@colspan">
<xsl:copy>
<xsl:copy-of select="@* except @colspan"/>
<xsl:apply-templates/>
</xsl:copy>
<xsl:for-each select="2 to @colspan">
<td/>
</xsl:for-each>
</xsl:when>
<xsl:otherwise>
<xsl:copy-of select="." />
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<!-- make sure it works for both table/tr and table/tbody/tr -->
<xsl:template match="tbody|table[not(tbody)]" mode="rowspan">
<xsl:copy>
<xsl:copy-of select="tr[1]" />
<xsl:apply-templates select="tr[2]" mode="rowspan">
<xsl:with-param name="previousRow" select="tr[1]" />
</xsl:apply-templates>
</xsl:copy>
</xsl:template>
<xsl:template match="tr" mode="rowspan">
<xsl:param name="previousRow" as="element()" />
<xsl:variable name="currentRow" select="." />
<xsl:variable name="normalizedTDs">
<xsl:for-each select="$previousRow/*">
<xsl:choose>
<xsl:when test="@rowspan > 1">
<xsl:copy>
<xsl:attribute name="rowspan">
<xsl:value-of select="@rowspan - 1" />
</xsl:attribute><!--
<xsl:copy-of select="@*[not(name() = 'rowspan')]" />
<xsl:copy-of select="node()" />
--></xsl:copy>
</xsl:when>
<xsl:otherwise>
<xsl:copy-of select="$currentRow/*[1 + count(current()/preceding-sibling::*[not(@rowspan) or (@rowspan = 1)])]" />
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</xsl:variable>
<xsl:variable name="newRow" as="element(tr)">
<xsl:copy>
<xsl:copy-of select="$currentRow/@*" />
<xsl:copy-of select="$normalizedTDs" />
</xsl:copy>
</xsl:variable>
<xsl:copy-of select="$newRow" />
<xsl:apply-templates select="following-sibling::tr[1]" mode="rowspan">
<xsl:with-param name="previousRow" select="$newRow" />
</xsl:apply-templates>
</xsl:template>
<xsl:template match="td | th" mode="final">
<xsl:choose>
<xsl:when test="@rowspan">
<xsl:copy>
<xsl:copy-of select="@* except @rowspan" />
<xsl:copy-of select="node()" />
</xsl:copy>
</xsl:when>
<xsl:otherwise>
<xsl:copy-of select="." />
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>