我想像这样转换表格:
<table>
<thead>
<tr>
<th rowspan="3">th11</th>
<th>th12</th>
<th rowspan="2">th13</th>
<th colspan="2">th14</th>
</tr>
<tr>
<th>th21</th>
<th>th22</th>
<th>th23</th>
</tr>
<tr>
<th>th31</th>
<th>th32</th>
<th>th33</th>
<th>th34</th>
</tr>
</thead>
</table>
到这张表:
<table>
<thead>
<tr>
<th rowspan="3">th11</th>
<th>th12</th>
<th rowspan="2">th13</th>
<th colspan="2">th14</th>
</tr>
<tr>
<th></th>
<th>th21</th>
<th></th>
<th>th22</th>
<th>th23</th>
</tr>
<tr>
<th></th>
<th>th31</th>
<th>th32</th>
<th>th33</th>
<th>th34</th>
</tr>
</thead>
</table>
实际上我想根据rowspan数字将空单元格放在带有rowspan的单元格下面。细胞&#39; rowspan和colspan属性应该出现在最终结果中。
<xsl:for-each select="tr">
<xsl:value-of select="sum(th[@colspan]/@colspan) + count(th[not(@colspan)])"/>
</xsl:for-each>
我希望这段代码能给我相同的数字。
如何使用XSLT执行此操作?为此目的适当的算法是什么?任何帮助将不胜感激。
答案 0 :(得分:1)
Convert HTML colspan and rowspan to "empty" cells中发布的XSLT 2.0解决方案的自适应(删除用于处理colspan
但添加用于处理thead
的代码的代码)如下
<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_rowspans">
<xsl:apply-templates mode="rowspan"/>
</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>
<!-- make sure it works for both table/tr and table/tbody/tr and table/thead/tr -->
<xsl:template match="table/tbody | table[not(tbody | thead)] | table/thead" 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>