如何在 XML 中转义 & 符号,以便它们在 HTML 中呈现为实体?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1328538/
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
How do I escape ampersands in XML so they are rendered as entities in HTML?
提问by AJM
I have some XML text that I wish to render in an HTML page. This text contains an ampersand, which I want to render in its entity representation: &
.
我有一些希望在 HTML 页面中呈现的 XML 文本。此文本包含一个&符号,我想在其实体表示中呈现:&
。
How do I escape this ampersand in the source XML? I tried &
, but this is decoded as the actual ampersand character (&
), which is invalid in HTML.
如何在源 XML 中转义此与号?我试过了&
,但它被解码为实际的 & 字符 ( &
),这在 HTML中是无效的。
So I want to escape it in such a way that it will be rendered as &
in the web page that uses the XML output.
所以我想以这样的方式转义它,它会像&
在使用 XML 输出的网页中一样呈现。
采纳答案by Wim ten Brink
When your XML contains &
, this will result in the text &
.
当您的 XML 包含时&
,这将导致文本&
。
When you use that in HTML, that will be rendered as &
.
当您在 HTML 中使用它时,它将呈现为&
.
回答by John Feminella
As per §2.4 of the XML 1.0 spec, you should be able to use &
.
根据XML 1.0 规范的 §2.4,您应该能够使用&
.
I tried & but this isn't allowed.
我试过 & 但这是不允许的。
Are you sure it isn't a different issue? XML explicitly defines this as the way to escape ampersands.
你确定这不是一个不同的问题?XML 明确地将此定义为转义符号的方法。
回答by trouble
The &
character is itself an escape character in XML so the solution is to concatenate it and a Unicode decimal equivalent for &
thus ensuring that there are no XML parsing errors. That is, replace the character &
with &
.
该&
字符本身是 XML 中的转义字符,因此解决方案是将它与 Unicode 十进制等价物连接起来,以&
确保没有 XML 解析错误。也就是说,替换字符&
用&
。
回答by scragar
Use CDATA
tags:
使用CDATA
标签:
<![CDATA[
This is some text with ampersands & other funny characters. >>
]]>
回答by nikc.org
&
should work just fine, Wikipedia has a List of predefined entities in XML.
&
应该可以正常工作,维基百科有一个XML 格式的预定义实体列表。
回答by Serhat Akay
In my case I had to change it to %26
.
就我而言,我不得不将其更改为%26
.
I needed to escape &
in a URL. So &
did not work out for me.
The urlencodefunction changes &
to %26
. This way neither XML nor the browser URL mechanism complained about the URL.
我需要&
在 URL 中转义。所以&
不适合我。将进行urlencode函数更改&
到%26
。这样一来,无论是 XML 还是浏览器 URL 机制都不会抱怨 URL。
回答by mcampos
I have tried &, but it didn't work. Based on Wim ten Brink's answer I tried &amp and it worked.
我试过&,但没有用。根据 Wim 10 Brink 的回答,我尝试了 & 并且它奏效了。
One of my fellow developers suggested me to use & and that worked regardless of how many times it may be rendered.
我的一位开发人员建议我使用 & 并且无论渲染多少次都有效。
回答by Riley Major
&
is the way to represent an ampersand in most sections of an XML document.
&
是在 XML 文档的大多数部分中表示与号的方式。
If you want to have XML displayed within HTML, you need to first create properly encoded XML (which involves changing &
to &
) and thenuse that to create properly encoded HTML (which involves again changing &
to &
). That results in:
如果您希望在 HTML 中显示 XML,您需要首先创建正确编码的 XML(涉及更改&
为&
),然后使用它来创建正确编码的 HTML(这涉及再次更改&
为&
)。这导致:
&amp;
For a more thorough explanation of XML encoding, see:
有关 XML 编码的更详尽说明,请参阅:
回答by Rick
<xsl:text disable-output-escaping="yes">& </xsl:text>
will do the trick.
<xsl:text disable-output-escaping="yes">& </xsl:text>
会做的伎俩。
回答by Scott Biggs
How about using the unicode \u0026
? Works for me in my android XML files. If problems arise, someone let me know.
使用 unicode\u0026
怎么样?在我的 android XML 文件中对我有用。如果出现问题,有人告诉我。