xml XSLT 特殊字符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2926028/
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
XSLT special characters
提问by Apurv
In the following XSL transformation how do I output the '<' and '>' symbol?
在以下 XSL 转换中,如何输出“<”和“>”符号?
Input XML:
输入 XML:
<TestResult bugnumber="3214" testname="display.methods::close->test_ManyInvoke" errortype="Failure"><ErrorMessage><![CDATA[calling close() method failed - expected:<2>]]></ErrorMessage>
XSLT:
XSLT:
<xsl:template match="TestResult">
<xsl:variable name="errorMessage">
<xsl:value-of select="ErrorMessage" disable-output-escaping="yes"/>
</xsl:variable>
<Test name='{@testname}'>
<TestResult>
<Passed>false</Passed>
<State>failure</State>
<Metadata>
<Entry name='bugnumber' value='{@bugnumber}' />
</Metadata>
<TestOutput>
<Metadata>
<Entry name='ErrorMessage' value='{$errorMessage}' />
</Metadata>
</TestOutput>
</TestResult>
</Test>
</xsl:template>
Output XML:
输出 XML:
<Test name="display.methods::close->test_ManyInvoke">
<TestResult>
<Passed>false</Passed>
<State>failure</State>
<Metadata>
<Entry name="bugnumber" value="3214"/>
</Metadata>
<TestOutput>
<Metadata>
<Entry name="ErrorMessage" value="calling close() method failed - expected:<2>"/>
</Metadata>
</TestOutput>
</TestResult>
</Test>
回答by Dimitre Novatchev
Short answer: You can't.
简短的回答:你不能。
Long answer: The value of attributes cannot contain a few special characters, such as '<', '>'and '&'.
长答案:属性的值不能包含几个特殊字符,例如'<','>'和'&'。
If present, they are escaped as: '<', '>'and '&'.
如果存在,它们将转义为:'<','>'和'&'。
These characters can be produced if the output method is 'text', which is not your case.
如果输出方法是“文本”,则可以生成这些字符,这不是您的情况。
Text that looks like these characters when displayed in a browser can also be produced: use: '<', '>'and '&'.
看起来像这些字符时,在浏览器中显示的文字也可以生产:用途:'<','>'和'&'。
Finally, you can produce these characters as part of a text node using CDATA section. The following example illustrates this:
最后,您可以使用 CDATA 部分将这些字符生成为文本节点的一部分。以下示例说明了这一点:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
<xsl:output cdata-section-elements="t"
omit-xml-declaration="yes" indent="yes"/>
<xsl:template match="/">
<t> < & ></t>
</xsl:template>
</xsl:stylesheet>
When this transformation is applied on any XML document (not used), the result is:
当此转换应用于任何 XML 文档(未使用)时,结果是:
<t><![CDATA[ < & >]]></t>
回答by Robert Rossney
If the fact that >is showing up in your attributes is a problem, the problem is with how the XML is being consumed, not how it's being produced.
如果>出现在您的属性中的事实是一个问题,那么问题在于 XML 的使用方式,而不是它的生成方式。
Any XML parser will unescape escaped character entities when it's used to retrieve an attribute value. If you use an XML parser in the consumption of an XML document, you'll never see this; the parser will tell you that the value of the attribute is display.methods::close->test_ManyInvoke.
任何 XML 解析器在用于检索属性值时都会对转义的字符实体进行转义。如果您在 XML 文档的使用中使用 XML 解析器,您将永远不会看到这一点;解析器会告诉你该属性的值为display.methods::close->test_ManyInvoke.
It seems likely to me that this "problem" is only appearing for one of two reasons:
在我看来,这个“问题”可能只是出于以下两个原因之一出现:
Someone who doesn't understand how XML works is looking at the output and saying, "Hey, it shouldn't be doing that."
Someone who doesn't understand how XML works has written his own logic to parse XML. ("Why should I use somebody else's XML parser when I can just write a simple regular expression?" Well, this is one reason why.)
不了解 XML 工作原理的人正在查看输出并说:“嘿,它不应该那样做。”
不了解 XML 工作原理的人编写了自己的逻辑来解析 XML。(“当我只能编写一个简单的正则表达式时,为什么还要使用其他人的 XML 解析器?”嗯,这就是原因之一。)
As a general rule of thumb, if you ever find yourself using disable-output-escaping='yes'in XSLT, or creating CDATA sections, you're probably doing the wrong thing. It's not certain- there are appropriate uses for both - but in my experience, the vast majority of time if the answer is "use CDATA" you're asking the wrong question.
作为一般经验法则,如果您发现自己disable-output-escaping='yes'在 XSLT 中使用或创建 CDATA 部分,那么您可能做错了事情。这不是一定的-有相应的用途都-但在我的经验中,绝大多数的时间,如果答案是“使用CDATA”你问错了问题。
回答by GMK
This is a really old question, and the other commenters are probably right to question why you need this, but if you just want to know how to add a < or > into your output here's how you do it.
这是一个非常古老的问题,其他评论者可能会质疑您为什么需要这个问题,但如果您只想知道如何在输出中添加 < 或 >,这里是您的方法。
<xsl:value-of disable-output-escaping="yes" select="string('<')"/>
More info at the MSDN site.
更多信息请访问MSDN 站点。
回答by Ely Bascoy
Be careful if you're trying to escape anything besides an ampersand within a string literal. For example,
如果您试图在字符串文字中转义除&符号之外的任何内容,请务必小心。例如,
test="$some_variable = 'Wendy's'"
would not work. You have to escape that first ampersand in the escape string, so you'd need to use
不会工作。您必须转义转义字符串中的第一个&符号,因此您需要使用
test="$some_variable = 'Wendy&apos;s'

