xsl:character-map 替换特殊字符 [英] xsl:character-map to replace special characters

查看:41
本文介绍了xsl:character-map 替换特殊字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定一个值为:

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

我需要将 &amp;lt;&amp;gt; 变成 &lt;&gt;因为下游应用程序需要在整个 XML 文档中采用这种格式.我也需要这个用于引号和撇号.我正在 XSLT 2.0 中尝试字符映射.

I need to turn &amp;lt; or &amp;gt; into &lt; or &gt; because a downstream app requires it in this format throughout the entire XML document. I would need this for quotes and apostrophes too. I am tryinging a character-map in XSLT 2.0.

<xsl:character-map name="specialchar">
    <xsl:output-character character="&apos;" string="&amp;apos;" />
    <xsl:output-character character="&quot;" string="&amp;quot;" />
    <xsl:output-character character="&gt;" string="&amp;gt;" />
</xsl:character-map>

推荐答案

指令可用于将单个字符序列化为任何字符串.然而,这个问题需要多个字符(一个 & 号后跟另一个要替换的字符.

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.

这里有一个解决这个问题的方法,使用 XPath 2.0 replace() 函数:

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>

当此转换应用于以下 XML 文档时:

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

产生想要的结果:

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

这篇关于xsl:character-map 替换特殊字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆