XML 中的“独立”指令是什么意思?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5578645/
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
What does the 'standalone' directive mean in XML?
提问by Nick
What does the 'standalone' directive mean in an XML document?
' standalone' 指令在 XML 文档中是什么意思?
采纳答案by nullability
The standalonedeclaration is a way of telling the parser to ignore any markup declarations in the DTD. The DTD is thereafter used for validation only.
该standalone声明是一种告诉解析器忽略DTD 中任何标记声明的方法。此后 DTD 仅用于验证。
As an example, consider the humble <img>tag. If you look at the XHTML 1.0 DTD, you see a markup declaration telling the parser that <img>tags must be EMPTY and possess srcand altattributes. When a browser is going through an XHTML 1.0 document and finds an <img>tag, it should notice that the DTD requires srcand altattributes and add them if they are not present. It will also self-close the <img>tag since it is supposed to be EMPTY. This is what the XML specificationmeans by "markup declarations can affect the content of the document." You can then use the standalonedeclaration to tell the parser to ignore these rules.
举个例子,考虑一下不起眼的<img>标签。如果您查看XHTML 1.0 DTD,您会看到一个标记声明,它告诉解析器<img>标签必须是 EMPTY 并且拥有src和alt属性。当浏览器浏览 XHTML 1.0 文档并找到一个<img>标签时,它应该注意到 DTD 需要src和alt属性,如果它们不存在,则添加它们。它也会自动关闭<img>标签,因为它应该是空的。这就是XML 规范所说的“标记声明可以影响文档的内容”的意思。然后您可以使用standalone声明来告诉解析器忽略这些规则。
Whether or not your parser actually does this is another question, but a standards-compliant validating parser (like a browser) should.
您的解析器是否真的这样做是另一个问题,但符合标准的验证解析器(如浏览器)应该这样做。
Note that if you do not specify a DTD, then the standalone declaration "has no meaning," so there's no reason to use it unless you also specify a DTD.
请注意,如果您没有指定 DTD,那么独立声明“没有意义”,因此除非您还指定了 DTD,否则没有理由使用它。
回答by Rinke
- The standalone directive is an optional attribute on the XML declaration.
- Valid values are
yesandno, wherenois the default value. - The attribute is only relevant when a DTDis used. (The attribute is irrelevant when using a schemainstead of a DTD.)
standalone="yes"means that the XML processor must use the DTD for validation only.In that case it will notbe used for:- default values for attributes
- entity declarations
- normalization
- Note that
standalone="yes"may add validity constraints if the document uses an external DTD. When the document contains things that would require modification of the XML, such as default values for attributes, andstandalone="yes"is used then the document is invalid. standalone="yes"may help to optimize performance of document processing.
- Standalone 指令是 XML 声明中的一个可选属性。
- 有效值为
yesandno,其中no是默认值。 - 该属性仅在使用DTD时相关。(当使用模式而不是 DTD时,该属性无关紧要。)
standalone="yes"意味着 XML 处理器必须仅使用 DTD 进行验证。在这种情况下,它将不会用于:- 属性的默认值
- 实体声明
- 正常化
- 请注意,
standalone="yes"如果文档使用外部 DTD ,则可能会添加有效性约束。当文档包含需要修改 XML 的内容(例如属性的默认值)standalone="yes"并被使用时,则文档无效。 standalone="yes"可能有助于优化文档处理的性能。
Source: The standalone pseudo-attribute is only relevant if a DTD is used
回答by Stefan Gehrig
standalonedescribes if the current XML document depends on an external markup declaration.
standalone描述当前 XML 文档是否依赖于外部标记声明。
W3C describes its purpose in "Extensible Markup Language (XML) 1.0 (Fifth Edition)":
W3C 在“可扩展标记语言 (XML) 1.0(第五版)”中描述了它的用途:
回答by Chris Diver
Markup declarations can affect the content of the document, as passed from an XML processor to an application; examples are attribute defaults and entity declarations. The standalone document declaration, which may appear as a component of the XML declaration, signals whether or not there are such declarations which appear external to the document entity or in parameter entities. [Definition: An external markup declaration is defined as a markup declaration occurring in the external subset or in a parameter entity (external or internal, the latter being included because non-validating processors are not required to read them).]
标记声明会影响文档的内容,就像从 XML 处理器传递到应用程序一样;示例是属性默认值和实体声明。独立文档声明可能作为 XML 声明的一个组成部分出现,表明是否存在出现在文档实体外部或参数实体中的此类声明。[定义:外部标记声明被定义为出现在外部子集或参数实体中的标记声明(外部或内部,后者被包括在内,因为不需要非验证处理器读取它们)。]
回答by user657267
The intent of the standalone=yesdeclaration is to guarantee that the information inside the document can be faithfully retrieved based only on the internal DTD, i.e. the document can "stand alone" with no external references. Validating a standalone document ensures that non-validating processors will have all of the information available to correctly parse the document.
standalone=yes声明的目的是保证文档内部的信息可以仅根据内部 DTD 进行忠实检索,即文档可以“独立”而没有外部引用。验证独立文档可确保非验证处理器拥有正确解析文档所需的所有信息。
The standalone declaration serves no purpose if a document has no external DTD, and the internal DTD has no parameter entity references, as these documents are already implicitly standalone.
如果文档没有外部 DTD,并且内部 DTD 没有参数实体引用,则独立声明没有任何作用,因为这些文档已经隐式独立。
The following are the actual effects of using standalone=yes.
以下是使用的实际效果standalone=yes。
Forces processors to throw an error when parsing documents with an external DTD or parameter entity references, if the document contains references to entities not declared in the internal DTD (with the exception of replacement text of parameter entities as non-validating processors are not required to parse this);
amp,lt,gt,apos, andquotare the only exceptionsWhen parsing a document not declared as standalone, a non-validating processor is free to stop parsing the internal DTD as soon as it encounters a parameter entity reference. Declaring a document as standalone forces non-validating processors to parse markup declarations in the internal DTD even after they ignore one or more parameter entity references.
Forces validating processors to throw an error if any of the following are found in the document, and their respective declarations are in the external DTD or in parameter entity replacement text:
- attributes with default values, if they do not have their value explicitly provided
- entity references (other than
amp,lt,gt,apos, andquot) - attributes with tokenized types, if the value of the attribute would be modified by normalization
- elements with element content, if any white space occurs in their content
强制处理器在解析具有外部 DTD 或参数实体引用的文档时抛出错误,如果文档包含对未在内部 DTD 中声明的实体的引用(参数实体的替换文本除外,因为非验证处理器不需要解析这个);
amp,lt,gt,apos, 和quot是唯一的例外当解析未声明为独立的文档时,非验证处理器可以在遇到参数实体引用时立即停止解析内部 DTD。将文档声明为独立文档会强制非验证处理器解析内部 DTD 中的标记声明,即使它们忽略了一个或多个参数实体引用。
如果在文档中发现以下任何一项,并且它们各自的声明在外部 DTD 或参数实体替换文本中,则强制验证处理器抛出错误:
- 具有默认值的属性,如果它们没有明确提供它们的值
- 实体引用(比其他
amp,lt,gt,apos,和quot) - 具有标记化类型的属性,如果属性的值将通过规范化修改
- 具有元素内容的元素,如果它们的内容中出现任何空白
A non-validating processor might consider retrieving the external DTD and expanding all parameter entity references for documents that are not standalone, even though it is under no obligation to do so, i.e. setting standalone=yescould theoretically improve performance for non-validating processors (spoiler alert: it probably won't make a difference).
非验证处理器可能会考虑检索外部 DTD 并扩展非独立文档的所有参数实体引用,即使它没有义务这样做,即设置standalone=yes理论上可以提高非验证处理器的性能(剧透警告:它可能不会有什么不同)。
The other answers here are either incomplete or incorrect, the main misconception is that
这里的其他答案要么不完整要么不正确,主要的误解是
The standalone declaration is a way of telling the parser to ignore any markup declarations in the DTD. The DTD is thereafter used for validation only.
独立声明是一种告诉解析器忽略 DTD 中任何标记声明的方法。此后 DTD 仅用于验证。
standalone="yes" means that the XML processor must use the DTD for validation only.
standalone="yes" 意味着 XML 处理器必须仅使用 DTD 进行验证。
Quite the opposite, declaring a document as standalone will actually force a non-validating processor to parse internal declarations it must normally ignore (i.e. those after an ignored parameter entity reference). Non-validating processors must still use the info in the internal DTD to provide default attribute values and normalize tokenized attributes, as this is independent of validation.
恰恰相反,将文档声明为独立的实际上会强制非验证处理器解析它通常必须忽略的内部声明(即那些在被忽略的参数实体引用之后的声明)。非验证处理器仍必须使用内部 DTD 中的信息来提供默认属性值和规范化标记化属性,因为这与验证无关。

