xml 如何在 XSLT 中转义字符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3415728/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 13:13:58  来源:igfitidea点击:

How do I escape a character in XSLT

xmlxsltescaping

提问by MoreCoffee

I have an XSLT that transforms a XML to PLSQL

我有一个将 XML 转换为 PLSQL 的 XSLT

I need to escape the character: > (greater than)

我需要转义字符:>(大于)

ex:

前任:

P_C710_INT_PROFILE_ID =>

I tried using >and putting the character in xsl:text with no luck

我尝试使用>并将字符放入 xsl:text 中,但没有运气

Any ideas?

有任何想法吗?

Thanks

谢谢

回答by MoreCoffee

Thank everyone but the correct answer is this:

谢谢大家,但正确答案是这样的:

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

回答by MoreCoffee

There is no problem. This stylesheet (empty):

没有问题。此样式表(空):

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

Input:

输入:

<text>P_C710_INT_PROFILE_ID =&gt;</text>

Output:

输出:

P_C710_INT_PROFILE_ID =>

EDIT: Because your question is not clear, I'm adding a solution in case you want to output character entity under a xsl:output/@method="text" declaration.

编辑:因为您的问题不清楚,所以我添加了一个解决方案,以防您想在 xsl:output/@method="text" 声明下输出字符实体。

This stylesheet:

这个样式表:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="text"/>
    <xsl:template match="text()" name="text">
        <xsl:param name="text" select="."/>
        <xsl:if test="$text != ''">
            <xsl:variable name="first" select="substring($text,1,1)"/>
            <xsl:choose>
                <xsl:when test="$first = '&gt;'">&amp;gt;</xsl:when>
                <xsl:otherwise>
                    <xsl:value-of select="$first"/>
                </xsl:otherwise>
            </xsl:choose>
            <xsl:call-template name="text">
                <xsl:with-param name="text" select="substring($text,2,(string-length($text)-1) div 2 + 1)"/>
            </xsl:call-template>
            <xsl:call-template name="text">
                <xsl:with-param name="text" select="substring($text,(string-length($text)-1) div 2 + 3)"/>
            </xsl:call-template>
        </xsl:if>
    </xsl:template>
</xsl:stylesheet>

Output:

输出:

P_C710_INT_PROFILE_ID =&gt;

This relate to Render escaped XML on browser

这与在浏览器上渲染转义 XML 有关

回答by robert

I was struggling with this as well and even after changing the output method and adding the disable-output-escaping attribute and it still wasnt working.

我也在为此苦苦挣扎,即使在更改了输出方法并添加了禁用输出转义属性之后,它仍然无法正常工作。

Then i looked in my source code and i realized that i was using an XmlTextWriter with the results of the transform and the XmlTextWriter was applying the output escaping. Once i switched it to just use a StringWriter the output was correct.

然后我查看了我的源代码,我意识到我使用的是带有转换结果的 XmlTextWriter,而 XmlTextWriter 正在应用输出转义。一旦我将其切换为仅使用 StringWriter,输出就正确了。