Replacing style= attributes with tags in XHTML via

2019-05-24 23:47发布

Say I have the following in an XHTML page:

<span style="color:#555555; font-style:italic">some text</span>

How would I go about transforming this to:

<span style="color:#555555;"><em>some text</em></span>

2条回答
孤傲高冷的网名
2楼-- · 2019-05-25 00:06

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>
查看更多
Melony?
3楼-- · 2019-05-25 00:22

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>
查看更多
登录 后发表回答