“独立”指令在XML文档中是什么意思?


standalone描述当前XML文档是否依赖于外部标记声明。

W3C在“可扩展标记语言(XML) 1.0(第五版)”中描述了它的目的。:

2.9独立文档声明

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).]

http://www.w3.org/TR/xml/#sec-rmd

独立声明是告诉解析器忽略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 src and alt attributes. When a browser is going through an XHTML 1.0 document and finds an <img> tag, it should notice that the DTD requires src and alt attributes 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 specification means by "markup declarations can affect the content of the document." You can then use the standalone declaration to tell the parser to ignore these rules.

您的解析器是否真正做到这一点是另一个问题,但是符合标准的验证解析器(比如浏览器)应该做到。

请注意,如果您没有指定DTD,那么独立声明“没有意义”,因此没有理由使用它,除非您还指定了DTD。

The standalone directive is an optional attribute on the XML declaration. Valid values are yes and no, where no is the default value. The attribute is only relevant when a DTD is used. (The attribute is irrelevant when using a schema instead of a DTD.) standalone="yes" means that the XML processor must use the DTD for validation only. In that case it will not be 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, and standalone="yes" is used then the document is invalid. standalone="yes" may help to optimize performance of document processing.

源:独立伪属性仅在使用DTD时才相关

standalone=yes声明的目的是保证文档中的信息可以仅根据内部DTD忠实地检索到,即文档可以“独立”,没有外部引用。验证独立文档可确保非验证处理程序拥有正确解析文档所需的所有信息。

如果文档没有外部DTD,内部DTD没有参数实体引用,那么独立声明就没有任何意义,因为这些文档已经是隐式独立的。

下面是使用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, and quot are the only exceptions When 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, and quot) 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并扩展非独立文档的所有参数实体引用,即使它没有义务这样做,即设置standalone=yes理论上可以提高非验证处理器的性能(剧剧警告:这可能不会有什么不同)。


这里的其他答案不是不完整就是不正确,主要的误解是

独立声明是告诉解析器忽略DTD中的任何标记声明的一种方式。DTD随后仅用于验证。

standalone="yes"意味着XML处理器必须只使用DTD进行验证。

恰恰相反,将文档声明为独立文档实际上会迫使非验证处理器解析它通常必须忽略的内部声明(即那些在被忽略的参数实体引用之后的声明)。非验证处理器仍然必须使用内部DTD中的信息来提供默认属性值和规范化标记化属性,因为这与验证无关。