Xslt XSL:读取DIV类属性以创建HTML表属性
我需要使用XSLT1.0将基于div的XHTML布局转换为基于表的布局。对于基本转换,我有一个样式表(如下所示),它可以很好地创建表结构 我不知道如何解析输入XHTML上的多个类属性,以便将特定于表的属性添加到输出中。(是的,我希望这些属性作为新的表属性,即使类是跨多个表复制的) 我的示例XHTML是:Xslt XSL:读取DIV类属性以创建HTML表属性,xslt,xslt-1.0,Xslt,Xslt 1.0,我需要使用XSLT1.0将基于div的XHTML布局转换为基于表的布局。对于基本转换,我有一个样式表(如下所示),它可以很好地创建表结构 我不知道如何解析输入XHTML上的多个类属性,以便将特定于表的属性添加到输出中。(是的,我希望这些属性作为新的表属性,即使类是跨多个表复制的) 我的示例XHTML是: <div class="table align-center"> <div class="tr"> <div class="td">&
<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>
表单元格1
表单元格2
构建表结构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>
表单元格1
表单元格2
我想制作的是:
<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>
表单元格1
表单元格2
使用XSLT1.0可以做到这一点吗?我希望解决方案足够通用,可以添加2个或更多类,并解析它们以添加所需的表属性
谢谢大家! 此XSLT 1.0样式表…
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" indent="yes"/>
<xsl:strip-space elements="*" />
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="div[starts-with(@class, 'table ')]">
<table>
<xsl:call-template name="extract-class">
<xsl:with-param name="class-list" select="normalize-space( substring-after(@class,'table '))" />
</xsl:call-template>
<xsl:apply-templates select="@*|node()"/>
</table>
</xsl:template>
<xsl:template match="div[starts-with(@class, 'tr ')]">
<tr>
<xsl:apply-templates select="@*|node()"/>
</tr>
</xsl:template>
<xsl:template match="div[starts-with(@class, 'td ')]">
<td>
<xsl:apply-templates select="@*|node()"/>
</td>
</xsl:template>
<xsl:template name="extract-class">
<xsl:param name="class-list" />
<xsl:if test="contains($class-list,'-')">
<xsl:variable name="name-value" select="substring-before(concat($class-list,' '),' ')" />
<xsl:attribute name="{substring-before($name-value,'-')}">
<xsl:value-of select="substring-after($name-value,'-')" />
</xsl:attribute>
<xsl:call-template name="extract-class">
<xsl:with-param name="class-list" select="normalize-space(substring-after($class-list,' '))" />
</xsl:call-template>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
<div class="table align-center border-1 cellspacing-5">
<div class="tr">
<div class="td"><p>Table Cell 1</p></div>
<div class="td"><p>Table Cell 2</p></div>
</div>
</div>
…应用于本文档时…
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" indent="yes"/>
<xsl:strip-space elements="*" />
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="div[starts-with(@class, 'table ')]">
<table>
<xsl:call-template name="extract-class">
<xsl:with-param name="class-list" select="normalize-space( substring-after(@class,'table '))" />
</xsl:call-template>
<xsl:apply-templates select="@*|node()"/>
</table>
</xsl:template>
<xsl:template match="div[starts-with(@class, 'tr ')]">
<tr>
<xsl:apply-templates select="@*|node()"/>
</tr>
</xsl:template>
<xsl:template match="div[starts-with(@class, 'td ')]">
<td>
<xsl:apply-templates select="@*|node()"/>
</td>
</xsl:template>
<xsl:template name="extract-class">
<xsl:param name="class-list" />
<xsl:if test="contains($class-list,'-')">
<xsl:variable name="name-value" select="substring-before(concat($class-list,' '),' ')" />
<xsl:attribute name="{substring-before($name-value,'-')}">
<xsl:value-of select="substring-after($name-value,'-')" />
</xsl:attribute>
<xsl:call-template name="extract-class">
<xsl:with-param name="class-list" select="normalize-space(substring-after($class-list,' '))" />
</xsl:call-template>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
<div class="table align-center border-1 cellspacing-5">
<div class="tr">
<div class="td"><p>Table Cell 1</p></div>
<div class="td"><p>Table Cell 2</p></div>
</div>
</div>
表单元格1
表单元格2
…产生…*
<table align="center" border="1" cellspacing="5" class="table align-center border-1 cellspacing-5">
<tr class="tr">
<td class="td">
<p>Table Cell 1</p>
</td>
<td class="td">
<p>Table Cell 2</p>
</td>
</tr>
</table>
表单元格1
表单元格2
此XSLT 1.0样式表…
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" indent="yes"/>
<xsl:strip-space elements="*" />
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="div[starts-with(@class, 'table ')]">
<table>
<xsl:call-template name="extract-class">
<xsl:with-param name="class-list" select="normalize-space( substring-after(@class,'table '))" />
</xsl:call-template>
<xsl:apply-templates select="@*|node()"/>
</table>
</xsl:template>
<xsl:template match="div[starts-with(@class, 'tr ')]">
<tr>
<xsl:apply-templates select="@*|node()"/>
</tr>
</xsl:template>
<xsl:template match="div[starts-with(@class, 'td ')]">
<td>
<xsl:apply-templates select="@*|node()"/>
</td>
</xsl:template>
<xsl:template name="extract-class">
<xsl:param name="class-list" />
<xsl:if test="contains($class-list,'-')">
<xsl:variable name="name-value" select="substring-before(concat($class-list,' '),' ')" />
<xsl:attribute name="{substring-before($name-value,'-')}">
<xsl:value-of select="substring-after($name-value,'-')" />
</xsl:attribute>
<xsl:call-template name="extract-class">
<xsl:with-param name="class-list" select="normalize-space(substring-after($class-list,' '))" />
</xsl:call-template>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
<div class="table align-center border-1 cellspacing-5">
<div class="tr">
<div class="td"><p>Table Cell 1</p></div>
<div class="td"><p>Table Cell 2</p></div>
</div>
</div>
…应用于本文档时…
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" indent="yes"/>
<xsl:strip-space elements="*" />
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="div[starts-with(@class, 'table ')]">
<table>
<xsl:call-template name="extract-class">
<xsl:with-param name="class-list" select="normalize-space( substring-after(@class,'table '))" />
</xsl:call-template>
<xsl:apply-templates select="@*|node()"/>
</table>
</xsl:template>
<xsl:template match="div[starts-with(@class, 'tr ')]">
<tr>
<xsl:apply-templates select="@*|node()"/>
</tr>
</xsl:template>
<xsl:template match="div[starts-with(@class, 'td ')]">
<td>
<xsl:apply-templates select="@*|node()"/>
</td>
</xsl:template>
<xsl:template name="extract-class">
<xsl:param name="class-list" />
<xsl:if test="contains($class-list,'-')">
<xsl:variable name="name-value" select="substring-before(concat($class-list,' '),' ')" />
<xsl:attribute name="{substring-before($name-value,'-')}">
<xsl:value-of select="substring-after($name-value,'-')" />
</xsl:attribute>
<xsl:call-template name="extract-class">
<xsl:with-param name="class-list" select="normalize-space(substring-after($class-list,' '))" />
</xsl:call-template>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
<div class="table align-center border-1 cellspacing-5">
<div class="tr">
<div class="td"><p>Table Cell 1</p></div>
<div class="td"><p>Table Cell 2</p></div>
</div>
</div>
表单元格1
表单元格2
…产生…*
<table align="center" border="1" cellspacing="5" class="table align-center border-1 cellspacing-5">
<tr class="tr">
<td class="td">
<p>Table Cell 1</p>
</td>
<td class="td">
<p>Table Cell 2</p>
</td>
</tr>
</table>
表单元格1
表单元格2
类属性值要求标记化,因此我想知道您是否可以使用。您的目标是哪个XSLT1.0处理器?嗨,Martin。我使用内置在PHP中的XSL过程。我认为不支持函数。类属性值要求标记化,所以我想知道您是否可以使用。您的目标是哪个XSLT1.0处理器?嗨,Martin。我使用内置在PHP中的XSL过程。我相信功能不受支持。哇!太好了。非常感谢你!哇!太好了。非常感谢你!