XML 中的引号。单人还是双人?

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

Quotes in XML. Single or double?

xmlformatting

提问by Sergey Metlov

I've heard that using single quotes to surround XML attribute values is a "bad style". Is this correct?

我听说使用单引号将 XML 属性值括起来是一种“糟糕的风格”。这样对吗?

Should I always write:

我应该总是写:

<element attr="value">

Or is it acceptable to write:

或者是可以接受的写法:

<element attr='value'>

Or does it not matter which style I use?

或者我使用哪种风格无关紧要?

回答by Oded

Both are legal. Choose one and stick with it. It doesn't matter.

两者都是合法的。选择一个并坚持下去。没关系。

From the spec:

规范

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

Showing that both are valid, as is mixing the two styles within an element, per attribute (though I suggest being consistent within any single document/set of documents).

显示两者都是有效的,就像在元素中混合两种样式一样,每个属性(尽管我建议在任何单个文档/文档集内保持一致)。

回答by Michael Kay

Double quotes are more usual, and it's quite acceptable for any particular community to adopt a house style for the sake of consistency, but a blanket statement that one way of doing it is better has no justification.

双引号更常见,对于任何特定社区来说,为了一致性而采用一种房屋风格是完全可以接受的,但笼统地声明一种方法更好是没有道理的。

It's also dangerous to make such recommendations, since it encourages the "desperate perl hackers" who try to parse XML using regular expressions instead of using a real XML parser, and invariably only succeed in handling a subset of what XML legally allows.

提出这样的建议也是危险的,因为它鼓励“绝望的 perl 黑客”,他们试图使用正则表达式而不是使用真正的 XML 解析器来解析 XML,并且总是只能成功处理 XML 合法允许的子集。

I tend to use single quotes for convenience if I'm hand-generating XML from Java applications - though I'm increasingly inclining to the view that hand-generating XML is almost as dangerous as hand-parsing it.

如果我从 Java 应用程序手动生成 XML,我倾向于使用单引号来方便起见——尽管我越来越倾向于认为手动生成 XML 几乎与手动解析它一样危险。