xsl:character-map to replace special characters

大城市里の小女人 提交于 2019-12-05 14:37:06

The <xsl:character-map> instruction can be used to serialize a single character to any string. However this problem requires more than one character (an ampersand followed by another character to be replaced.

<xsl:character-map> cannot be used to solve such problems.

Here is a solution to this problem, using the XPath 2.0 replace() function:

<xsl:stylesheet version="2.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output omit-xml-declaration="yes"/>

 <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(
                replace(., "&amp;lt;", "&lt;"),
                "&amp;gt;",
                "&gt;"
                ),
        "&amp;apos;",
        "&apos;"
        )
    '/>
 </xsl:template>
</xsl:stylesheet>

when this transformation is applied on the following XML document:

<xml_element>Distrib = SU &amp; &amp;apos;Prem &amp;lt;&amp;gt; 0</xml_element>

the wanted result is produced:

<xml_element>Distrib = SU &amp; 'Prem &lt;&gt; 0</xml_element>
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!