Because you said that the translate() function is successfully removing <sup>
and </sup>
, I am assuming that <sup>
is not an element in the XML document, but is encoded as text.
The translate() function is defined to substitute individual characters and generally isn't suitable for string replacement when the string length is greater than 1.
It is possible to write and use a general string replacement recursive template/function in XSLT.
XSLT 2.0 programmers can use the standard XPath 2.0 function replace().
In your particular case even this this may be sufficient:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="text()">
<xsl:variable name="vPart1" select=
"substring-before(., '<sup>')"/>
<xsl:value-of select="$vPart1"/>
<xsl:variable name="vPart2" select=
"substring-before(substring-after(., '<sup>'),
'</sup>'
)"/>
<xsl:value-of select="$vPart2"/>
<xsl:variable name="vPart3" select=
"substring-after(., '</sup>')"/>
<xsl:value-of select="$vPart3"/>
</xsl:template>
</xsl:stylesheet>
When this transformation is applied on the following XML document:
<name>
<![CDATA[sony Braiva <sup>tm</sup> xxx]]>
</name>
the wanted result is produced:
<name>
sony Braiva tm xxx
</name>
Alternatively, here is the full-blown recursive template solution:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="text()">
<xsl:variable name="vFirstReplacement">
<xsl:call-template name="replace">
<xsl:with-param name="pText" select="."/>
<xsl:with-param name="pPattern"
select="'<sup>'"/>
<xsl:with-param name="pReplacement" select="''"/>
</xsl:call-template>
</xsl:variable>
<xsl:call-template name="replace">
<xsl:with-param name="pText"
select="$vFirstReplacement"/>
<xsl:with-param name="pPattern"
select="'</sup>'"/>
<xsl:with-param name="pReplacement" select="''"/>
</xsl:call-template>
</xsl:template>
<xsl:template name="replace">
<xsl:param name="pText"/>
<xsl:param name="pPattern"/>
<xsl:param name="pReplacement"/>
<xsl:choose>
<xsl:when test="not(contains($pText, $pPattern))">
<xsl:value-of select="$pText"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select=
"substring-before($pText, $pPattern)"/>
<xsl:value-of select="$pReplacement"/>
<xsl:call-template name="replace">
<xsl:with-param name="pText" select=
"substring-after($pText, $pPattern)"/>
<xsl:with-param name="pPattern"
select="$pPattern"/>
<xsl:with-param name="pReplacement"
select="$pReplacement"/>
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
When this transformation is applied on this XML document:
<name>
<![CDATA[sony Braiva <sup>tm</sup> xxx]]>
</name>
the wanted, correct result is produced:
<name>
sony Braiva tm xxx
</name>
Finally, here is the XSLT 2.0 solution:
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="text()">
<xsl:value-of select=
"replace(
replace(., '<sup>', ''),
'</sup>',
''
)
"/>
</xsl:template>
</xsl:stylesheet>