本文介绍了通过 XSLT 用 XHTML 中的标签替换 style= 属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假设我在 XHTML 页面中有以下内容:
Say I have the following in an XHTML page:
<span style="color:#555555; font-style:italic">some text</span>
我将如何将其转换为:
<span style="color:#555555;"><em>some text</em></span>
推荐答案
这并不像看起来那么容易,因为 XSLT 不是字符串解析的最佳工具 - 但这正是您获取样式属性内容所需要的对一般.
This is not as easy as it seems since XSLT is not the best tool for string parsing - but that's exactly what you need to get the contents of the style attribute right generically.
但是,根据您输入的复杂程度,这样的内容可能就足够了(不过,我尝试尽可能通用):
However, depending on the complexity of your input, something like this might be enough (I tried to be as generic as possible, though):
<!-- it's a good idea to build most XSLT around the identity template -->
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*" />
</xsl:copy>
</xsl:template>
<!-- specific templates over general ones with complex if/choose inside -->
<xsl:template match="span[
contains(translate(@style, ' ', ''), 'font-style:italic')
]">
<xsl:copy>
<xsl:copy-of select="@*" />
<xsl:attribute name="style">
<!-- this is pretty assumptious - might work, might break,
depending on how complex the @style value is -->
<xsl:value-of select="substring-before(@style, 'font-style')" />
<xsl:value-of select="substring-after(@style, 'italic')" />
</xsl:attribute>
<em>
<xsl:apply-templates select="node()" />
</em>
</xsl:copy>
</xsl:template>
这篇关于通过 XSLT 用 XHTML 中的标签替换 style= 属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!