[英]XSLT: Transforming HTML attributes into inline CSS
I have invalid HTML that I am trying to transform into valid HTML using an XSLT transformation. 我有无效的HTML,我正在尝试使用XSLT转换将其转换为有效的HTML。 For example, I want to turn some attributes into inline CSS.
例如,我想将一些属性转换为内联CSS。 Consider the following:
考虑以下:
<table border="1" id="t01" width="100%">
.
.
.
</table>
The border
and width
attributes on the table
element are obsolete. table
元素上的border
和width
属性已过时。 So I want to use inline CSS instead, like this: 所以我想改用内联CSS,如下所示:
<table style="border:1;width:100%;" id="t01">
.
.
.
</table>
How can I do this with XSLT? 如何使用XSLT做到这一点?
You could certainly make this prettier, more generic, etc., but my first pass would be something like: 您当然可以使它更漂亮,更通用,等等,但是我的第一步是这样的:
<xsl:template match="table">
<table>
<xsl:attribute name="style">
<xsl:if test="@border">
<xsl:value-of select="concat('border:', @border, '; ')"/>
</xsl:if>
<xsl:if test="@width">
<xsl:value-of select="concat('width:', @width, '; ')"/>
</xsl:if>
</xsl:attribute>
<xsl:copy-of select="@id"/>
<!-- either this or apply-templates -->
<xsl:copy-of select="*"/>
</table>
</xsl:template>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.