XML 属性中哪些字符无效(除非已编码)?

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

Which characters are Invalid (unless encoded) in an XML attribute?

xmlxml-serialization

提问by Euro Micelli

I can't believe I can't find this information easily accessible, so:

我不敢相信我无法轻松找到这些信息,所以:

1) Which characters cannot be incorporated in an XML attribute without entity-encoding them?

1) 哪些字符不能在没有实体编码的情况下合并到 XML 属性中?

Obviously, you need to encode quotes. What about <and >? What else?

显然,您需要对引号进行编码。怎么样<>?还有什么?

2) Where exactly is the official list?

2)官方名单具体在哪里?

回答by great_llama

Here is the definition of what is allowed in an attribute value.

这是属性值中允许的定义。

'"' ([^<&"] | Reference)* '"'  |  "'" ([^<&'] | Reference)* "'" 

So, you can't have:

所以,你不能有:

  • the same character that opens/closes the attribute value (either'or ")
  • a naked ampersand (&must be &amp;)
  • a left angle bracket (<must be &lt;)
  • 打开的相同的字符/关闭所述属性值(无论是'"
  • 一个赤裸的&符号(&必须是&amp;
  • 左尖括号(<必须是&lt;

You should also not being using any charactersthat are outright not legal anywhere in an XML document (such as form feeds, etc).

您也不应该在 XML 文档中的任何地方使用任何完全不合法的字符(例如表单提要等)。

回答by codehead

As per the (2) current recommendation, specifically regarding character data and Markup, they are (1) the ampersand (&), left angle bracket (<), right angle bracket (>) and both single-quote (') and double-quote (").

根据 (2)当前建议,特别是关于字符数据和标记,它们是 (1) 与号 (&)、左尖括号 (<)、右尖括号 (>) 以及单引号 (') 和双引号-引用 (”)。

回答by John Saunders

See 2.2 Charactersin "Extensible Markup Language (XML) 1.0 (Third Edition)".

2.2字符“可扩展标记语言(XML)1.0(第三版)”

Note that, at least with .NET, if you are using the XML APIs to work with XML, then you won't have to worry about this. It's the reason not to treat XML as being text.

请注意,至少对于 .NET,如果您使用 XML API 来处理 XML,那么您将不必担心这一点。这就是不将 XML 视为文本的原因。