如何强制将每个双引号“转义为 XML 字符实体”

How to force escaping of every double quote " into the XML character entity "

如何将字符串中存在的所有 " 转换为 "

示例:

源数据: <String>"ACCOUNT_DETAILS" : "75"</String>

目标数据: <String>&quot;ACCOUNT_DETAILS&quot; : &quot;75&quot;</String>

PS:我正在使用 XSLT 1.0。我尝试在“XSLT string replace”进行讨论,但没有成功。

你为什么要做这样的事情?从 XML 的角度来看,&quot;" 完全相同 。并且任何 XSLT 处理器(或与此相关的 XML 库)在单引号和双引号作为文字出现在属性内时都会正确转义,这通常是它们需要转义为 &apos;&quot;分别

但是,如果有一些奇怪的要求,您 必须 这样做(有很多工具只支持部分 XML 或其某些变体) 您可以执行以下操作:

<xsl:text disable-output-escaping="yes">&amp;quot;</xsl:text>

这将输出 &quot;。请注意,处理器不需要支持 disable-output-escaping,即使它支持,也不必服从,而且从 XSLT 2.0 开始,您应该使用 xsl:character-map 代替,这是一个更好的选择和更灵活的选择。

使用 XSLT 1.0 的字符串替换模板

您可能跟进的问题是如何将此技术应用于整个字符串。由于 XSLT 1.0 没有很好的字符串搜索和替换功能,您必须使用递归调用模板。对于练习(我对 XSLT 1.0 有点生疏,通常使用 XSLT 2.0 或更高版本,其中这是一个单行),我想为了旧时光再试一次。

这适用于您的输入:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="1.0">

    <xsl:variable name="input">"ACCOUNT_DETAILS" : "75"</xsl:variable>

    <xsl:template match="/">
        <xsl:variable name="result">
            <xsl:call-template name="replace">
                <xsl:with-param name="string" select="$input"/>
                <xsl:with-param name="search" select="'&quot;'" />
                <xsl:with-param name="replace-with" select="'&amp;quot;'" />
            </xsl:call-template>
        </xsl:variable>
        <xsl:value-of select="$result" disable-output-escaping="yes" />
    </xsl:template>

    <xsl:template name="replace">
        <xsl:param name="string" />
        <xsl:param name="search" />
        <xsl:param name="replace-with" />        
        <xsl:choose>
            <xsl:when test="contains($string, $search)">
                <xsl:value-of select="substring-before($string, $search)" />
                <xsl:value-of select="$replace-with" />
                <xsl:call-template name="replace">
                    <xsl:with-param name="string" select="substring-after($string, $search)" />
                    <xsl:with-param name="search" select="$search" />
                    <xsl:with-param name="replace-with" select="$replace-with" />
                </xsl:call-template>
            </xsl:when>
            <xsl:otherwise>
                <xsl:value-of select="$string" />
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>

</xsl:stylesheet>

并输出:

&quot;ACCOUNT_DETAILS&quot; : &quot;75&quot;

编辑:使用 JSON 转义 \"

在您写的评论中,您需要此 JSON。使用 JSON 转义可能更容易,因为它们在 XML.

中没有特殊含义

拿我上面的代码调用如下,去掉disable-output-escaping:

<xsl:call-template name="replace">
    <xsl:with-param name="string" select="$input"/>
    <xsl:with-param name="search" select="'&quot;'" />
    <xsl:with-param name="replace-with" select="'\&quot;'" />
</xsl:call-template>

或者,如果你想要 XML 风格的转义,而不是直接将 &quot; 写入输出流(这只是另一种写 " 的方式,将被解释因此,任何 XML 解析器)您可以使用上面相同的代码对引号进行双重转义,但不使用 disable-output-escaping。然后文字 XML 将包含 &amp;quot 并且您的浏览器或 XML 解析器会将其视为 &quot; (在未转义之后)。

我认为在 XSLT 2.0 中使用字符映射应该是可能的:http://xsltransform.net/pPzifoT/1。使用 XSLT 1.0,您可以使用命名模板,用 <xsl:text disable-output-escaping="yes"><![CDATA[&quot;]]></xsl:text>.

替换双引号