如何在 XML 文档中包含与号 (&) 字符?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14500800/
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:51:46  来源:igfitidea点击:

How can I include an ampersand (&) character in an XML document?

xmlvalidation

提问by nagy.zsolt.hun

How can I put an &symbol in an attribute of an XML tag while keeping the XML valid?

如何&在保持 XML 有效的同时将符号放入 XML 标记的属性中?

<?xml version="1.0" ?> 
<a text="b&c"/>

When I use W3School's validator, I get the error:

当我使用W3School 的验证器时,出现错误:

EntityRef: expecting ';'

EntityRef: 期待“ ;

回答by Quentin

Use a character reference to represent it: &amp;

使用字符引用来表示它: &amp;

See the specification:

规格

The ampersand character (&) and the left angle bracket (<) MUST NOT appear in their literal form, except when used as markup delimiters, or within a comment, a processing instruction, or a CDATA section. If they are needed elsewhere, they MUST be escaped using either numeric character references or the strings " &amp;" and " &lt;" respectively. The right angle bracket (>) may be represented using the string " &gt;", and MUST, for compatibility, be escaped using either " &gt;" or a character reference when it appears in the string " ]]>" in content, when that string is not marking the end of a CDATA section.

与符号 (&) 和左尖括号 (<) 不得以其文字形式出现,除非用作标记定界符,或者在注释、处理指令或 CDATA 部分中使用。如果其他地方需要它们,它们必须分别使用数字字符引用或字符串“ &amp;”和“ &lt;”进行转义 。右尖括号 (>) 可以使用字符串 " &gt;" 表示,并且为了兼容性,必须使用 " &gt;" 或出现在字符串 " 中的字符引用 进行转义 ]]>" 在内容中,当该字符串未标记 CDATA 部分的结尾时。

回答by Shubham

You can use these escape sequences :

您可以使用这些转义序列:

< (less-than)                   -   &#60; or &lt;

> (greater-than)                -   &#62; or  &gt;

& (ampersand)                   -   &#38;

' (apostrophe or single quote)  -   &#39;

" (double-quote)                -   &#34;