我需要使用 XSLT 1.0 将基于 div 的 XHTML 布局转换为基于表格的布局。对于基本转换,我有一个样式表(如下),可以很好地创建表结构。
我无法弄清楚如何解析输入 XHTML 上的多个类属性以将特定于表的属性添加到输出中。(是的,我希望这些作为新的表属性,即使类被复制)
我的示例 XHTML 是:
<div class="table align-center">
<div class="tr">
<div class="td"><p>Table Cell 1</p></div>
<div class="td"><p>Table Cell 2</p></div>
</div>
</div>
构建表结构OK的基本XSL如下:
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="div[contains(@class, 'table')]">
<table>
<xsl:copy-of select="attribute::node()"/>
<xsl:apply-templates/>
</table>
</xsl:template>
<xsl:template match="div[contains(@class, 'tr')]">
<tr>
<xsl:copy-of select="attribute::node()"/>
<xsl:apply-templates/>
</tr>
</xsl:template>
<xsl:template match="div[contains(@class, 'td')]">
<td>
<xsl:copy-of select="attribute::node()"/>
<xsl:apply-templates/>
</td>
</xsl:template>
此样式表产生:
<table class="table align-center">
<tr class="tr">
<td class="td"><p>Table Cell 1</p></td>
<td class="td"><p>Table Cell 2</p></td>
</tr>
</table>
我想制作的是这样的:
<table class="table align-center" align="center">
<tr class="tr">
<td class="td"><p>Table Cell 1</p></td>
<td class="td"><p>Table Cell 2</p></td>
</tr>
</table>
是否可以使用 XSLT 1.0 做到这一点?我希望解决方案足够通用,可以添加 2 个或更多类并解析它们以添加所需的表属性。
谢谢!