没有值的 xml 属性是否有效?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6926442/
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
Is an xml attribute without a value, valid?
提问by Andreas
I want to have an XML attribute without any value, which simply has one meaning when it exists or does not exist.
我想要一个没有任何值的 XML 属性,它在存在或不存在时只有一种含义。
Is that valid?
那有效吗?
回答by Dave DuPlantis
An attribute must be specified with the following syntax:
必须使用以下语法指定属性:
Name Eq AttValue
名称方程 AttValue
where Name is a legal XML name, Eq is = optionally preceded or followed by whitespace, and AttValue is a legal attribute value.
其中 Name 是一个合法的 XML 名称, Eq 是= 前面或后面可选的空格,而 AttValue 是一个合法的属性值。
This definition is true for both XML 1.0and XML 1.1.
此定义对XML 1.0和XML 1.1 均适用。
If you are trying to specify an attribute as below:
如果您尝试指定如下属性:
<car owned/>
then no, that is not valid. If you are trying to specify it this way:
那么不,那是无效的。如果您尝试以这种方式指定它:
<car owned=""/>
then yes, that is valid.
那么是的,这是有效的。
回答by Quentin
No.
不。
Boolean attributes in XML are of the form foo="foo".
XML 中的布尔属性采用foo="foo".
Even in SGML, you must provide the value, (it is the name, =and quotes that you can omit, which is why you have things like <select multiple>in HTML).
即使在 SGML 中,您也必须提供值(它是名称=和可以省略的引号,这就是为什么您<select multiple>在 HTML 中使用类似内容的原因)。
回答by Michael Kay
Yes. You can have an attribute whose only permitted value is the empty string, "". I'm not sure it's good design, though; I would normally suggest a boolean attribute with values true/false, and a default value of false.
是的。您可以拥有一个属性,其唯一允许的值是空字符串“”。不过,我不确定它是否是好的设计。我通常会建议一个布尔属性,其值为真/假,默认值为假。

![xml XQuery [value()]: 'value()' 需要一个单例(或空序列),找到类型为 'xdt:untypedAtomic *' 的操作数](/res/img/loading.gif)