Replacing style= attributes with tags in XHTML via XSLT

北战南征 提交于 2019-12-06 06:28:40

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>

Just for fun, a more general XSLT 2.0 solutions (can be optimized):

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="node()|@*">
        <xsl:copy>
            <xsl:apply-templates select="node()|@*"/>
        </xsl:copy>
    </xsl:template>
    <xsl:template
         match="span[tokenize(@style,';')[
                        matches(.,'\p{Z}*font\-style\p{Z}*:\p{Z}*italic\p{Z}*')
                     ]]">
        <xsl:copy>
            <xsl:apply-templates select="@* except @style"/>
            <xsl:attribute
                 name="style"
                 select=
                 "tokenize(@style,';')[not(
                     matches(.,'\p{Z}*font\-style\p{Z}*:\p{Z}*italic\p{Z}*')
                  )]"
                 separator=";"/>
            <em>
                <xsl:apply-templates select="node()"/>
            </em>
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

Output:

<span style="color:#555555"><em>some text</em></span>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!