XML 属性可以是空字符串吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/184014/
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
Can an XML attribute be the empty string?
提问by David Locke
Can an XML attribute be the empty string?
XML 属性可以是空字符串吗?
In other words, is
换句话说,是
<element att="" />
valid XML?
有效的 XML?
采纳答案by Mitchel Sellers
You can create an empty attribute via attname=""
您可以通过 attname="" 创建一个空属性
You can create an empty element via
您可以通过创建一个空元素
<elementName></elementName>
or
或者
<elementName/>
回答by Bradley Grainger
Yes, this is well-formed XML.
是的,这是格式良好的 XML。
An easy way to test this (on Windows) is to save the sample in a test.xmlfile and open it with Internet Explorer. IE will display an error message if the document is not well-formed.
对此进行测试的一种简单方法(在 Windows 上)是将示例保存在一个test.xml文件中并使用 Internet Explorer 打开它。如果文档格式不正确,IE 将显示错误消息。
回答by Greg Hewgill
It is worth nothing that this is an XML attribute, not an element. An empty element would be:
这是一个 XML属性,而不是一个元素,这是毫无价值的。空元素将是:
</>
which is not valid XML.
这不是有效的 XML。
回答by coder
this is valid xml tag:
这是有效的 xml 标记:
<mytag myattrib=""/>
this is not:
这不是:
<mytag myattrib/>
回答by coder
It should also be noted that some XML parsers will throw an error on empty string nodes and attributes instead of returning null or an empty string. So even though it might be valid, it would be better to leave it out altogether.
还应该注意的是,一些 XML 解析器会在空字符串节点和属性上抛出错误,而不是返回 null 或空字符串。因此,即使它可能有效,也最好将其完全排除。
回答by Michael Burr
Yes - element content can be empty, and as you used in your question there's even the special Empty-Element tag notation:
是的 - 元素内容可以为空,正如您在问题中使用的那样,甚至还有特殊的 Empty-Element 标记符号:
<elementname />
Except when interop with SGML is necessary, the above is equivalent to
除非需要与 SGML 互操作,否则以上等效于
<elementname></elementname>
Attribute values can also be empty, but attributes must always be followed by an '=' and a quoted string, even if the string contains no characters.
属性值也可以为空,但属性后必须始终跟有“=”和带引号的字符串,即使该字符串不包含任何字符。
The definitive place for this information is the XML spec: http://www.xml.com/axml/testaxml.htm
此信息的最终位置是 XML 规范:http: //www.xml.com/axml/testaxml.htm
Here's what the Annotated XML Reference has to say about empty elements:
以下是 Annotated XML Reference 关于空元素的说明:
So, is this:
<img src='madonna.gif'></img>really exactly the same as<img src='madonna.gif'/>? As far as XML is concerned, they are. This decision was the occasion of much religious debate, with some feeling that there is an essential element between "point" and "container" type elements. And as the "for interoperability" note below makes clear, if you are using pre-1998 SGML software to process XML, there is a big difference.
那么,这是:
<img src='madonna.gif'></img>真的完全一样<img src='madonna.gif'/>吗?就 XML 而言,它们是。这个决定引起了很多宗教争论,有些人觉得“点”和“容器”类型元素之间有一个基本元素。正如下面的“互操作性”说明所明确指出的,如果您使用 1998 年之前的 SGML 软件来处理 XML,就会有很大的不同。
note:the discussion on empty elements was due to the original wording of the posted question.
注意:关于空元素的讨论是由于发布问题的原始措辞。

