这是后续这个问题.
我有一些<span>
文档中的标签具有多个分号分隔的样式属性。现在我有 3 个特定的样式属性,我正在寻找将其转换为标签。只要样式属性仅包含三个样式属性之一,在上面的示例中一切都可以正常工作。如果一个跨度有更多,我会得到一个不明确的规则匹配。
我正在寻找的三个样式属性是font-style:italic
, font-weight:600
, and text-decoration:underline
应该从 style 属性中删除并转换为<em>
, <strong>
, and <u>
, 分别。
这是我当前的 XSLT:
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="span[
contains(translate(@style, ' ', ''), 'font-style:italic')
]">
<xsl:copy>
<xsl:attribute name="style">
<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>
<xsl:template match="span[
contains(translate(@style, ' ', ''), 'font-weight:600')
]">
<xsl:copy>
<xsl:attribute name="style">
<xsl:value-of select="substring-before(@style, ' font-weight')"/>
<xsl:value-of select="substring-after(@style, '600;')"/>
</xsl:attribute>
<strong>
<xsl:apply-templates select="node()"/>
</strong>
</xsl:copy>
</xsl:template>
<xsl:template match="span[
contains(translate(@style, ' ', ''), 'text-decoration:underline')
]">
<xsl:copy>
<xsl:attribute name="style">
<xsl:value-of select="substring-before(@style, ' text-decoration')"/>
<xsl:value-of select="substring-after(@style, 'underline;')"/>
</xsl:attribute>
<u>
<xsl:apply-templates select="node()"/>
</u>
</xsl:copy>
</xsl:template>
这将生成不明确的规则警告,该警告在包含多个列出的属性的某些元素上无法正常工作。
输入示例:
<span style=" text-decoration: underline; font-weight:600; color:#555555">some text</span>
转变为:
<span style=" font-weight:600; color:#555555"><u>some text</u></span>
当期望的结果是:
<span style="color:#555555"><b><u>some text</u></b></span>
我怎样才能解决这个不明确的规则匹配?
提前致谢
Update:
如果我设置priorty
将每个模板的值降序,并在第一次 XSLT 运行的输出上再次运行 XSLT,一切都按预期进行。必须有一种比运行两次转换更简单的方法。有任何想法吗?
正如亚历杭德罗和托马拉克建议的那样,替换style
属性与class
CSS 类的属性也是一个选项。