如何在 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
How can I include an ampersand (&) character in an XML document?
提问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: &
使用字符引用来表示它: &
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 "
&" and "<" respectively. The right angle bracket (>) may be represented using the string ">", and MUST, for compatibility, be escaped using either ">" 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 部分中使用。如果其他地方需要它们,它们必须分别使用数字字符引用或字符串“
&”和“<”进行转义 。右尖括号 (>) 可以使用字符串 ">" 表示,并且为了兼容性,必须使用 ">" 或出现在字符串 " 中的字符引用 进行转义]]>" 在内容中,当该字符串未标记 CDATA 部分的结尾时。
回答by Shubham
You can use these escape sequences :
您可以使用这些转义序列:
< (less-than) - < or <
> (greater-than) - > or >
& (ampersand) - &
' (apostrophe or single quote) - '
" (double-quote) - "

