如何在 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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 00:41:26  来源:igfitidea点击:

How do I escape ampersands in XML so they are rendered as entities in HTML?

htmlxmlescapingampersand

提问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 CDATAtags:

使用CDATA标签:

 <![CDATA[
   This is some text with ampersands & other funny characters. >>
 ]]>

回答by nikc.org

&amp;should work just fine, Wikipedia has a List of predefined entities in XML.

&amp;应该可以正常工作,维基百科有一个XML 格式的预定义实体列表

回答by Serhat Akay

In my case I had to change it to %26.

就我而言,我不得不将其更改为%26.

I needed to escape &in a URL. So &amp;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 中转义。所以&amp;不适合我。将进行urlencode函数更改&%26。这样一来,无论是 XML 还是浏览器 URL 机制都不会抱怨 URL。

回答by mcampos

I have tried &amp, but it didn't work. Based on Wim ten Brink's answer I tried &amp;amp and it worked.

我试过&,但没有用。根据 Wim 10 Brink 的回答,我尝试了 &amp 并且它奏效了。

One of my fellow developers suggested me to use &#x26; and that worked regardless of how many times it may be rendered.

我的一位开发人员建议我使用 & 并且无论渲染多少次都有效。

回答by Riley Major

&amp;is the way to represent an ampersand in most sections of an XML document.

&amp;是在 XML 文档的大多数部分中表示与号的方式。

If you want to have XML displayed within HTML, you need to first create properly encoded XML (which involves changing &to &amp;) and thenuse that to create properly encoded HTML (which involves again changing &to &amp;). That results in:

如果您希望在 HTML 中显示 XML,您需要首先创建正确编码的 XML(涉及更改&&amp;),然后使用它来创建正确编码的 HTML(这涉及再次更改&&amp;)。这导致:

&amp;amp;

For a more thorough explanation of XML encoding, see:

有关 XML 编码的更详尽说明,请参阅:

What characters do I need to escape in XML documents?

我需要在 XML 文档中转义哪些字符?

回答by Rick

<xsl:text disable-output-escaping="yes">&amp;&nbsp;</xsl:text>will do the trick.

<xsl:text disable-output-escaping="yes">&amp;&nbsp;</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 文件中对我有用。如果出现问题,有人告诉我。