Jesper Tverskov, Marts 15, 2011

The standalone pseudo-attribute is only relevant if a DTD is used

The standalone pseudo-attribute in the XML declaration is a mystery for many XML beginners. Most often it is irrelevant but it is tempting to add it or delete it or to change its value from "yes" to "no" to "yes" as long as you don't know what it is all about.

The XML declaration must have a "version" pseudo-attribute, but can also have an "encoding" and a "standalone" pseudo-attribute. The "standalone" pseudo-attribute is defined in a section called Standalone Document Declaration in the XML Recommendation (standard). [1]

  1. <?xml version="1.0" encoding="utf-8" standalone="yes"?>

Let us be more precise. A DTD can consist of an internal DTD subset declared in the top of the XML document inside the DTD declaration and/or an external DTD subset declared in a separate text document. Most often a DTD only consists of a DTD declaration in the top of an XML document, right after the XML declaration, pointing to the external DTD subset. The standalone pseudo-attribute could only be relevant if an external DTD subset exists.

The "standalone"-attribute is completely irrelevant if an XML document is not using a DTD. This is most often the case today. Most XML documents use an XML Schema schema instead of a DTD.

Since most XML documents don't use DTDs, it is pretty safe to make it into a rule of thumb for beginners: "Just don't bother about the standalone pseudo-attribute. If it shows up just ignore it, or delete it, and it does not matter if the value is "yes" or "no".

1. Standalone="no"

Only if the XML document has a DTD, the "standalone" pseudo-attribute could be of importance. If the XML declaration has no "standalone" pseudo-attribute it defaults to standalone="no". It is never necessary to use standalone="no" explicitly.

If the standalone pseudo-attribute is not used or if it is used explicitly with the value "no", it is a signal to the XML processor that the DTD is not just for validation: there could be things to look up in the DTD like:

The two items above are the important things to remember. But the XML spec also talks about normalization of attribute values and about ignorable whitespace in elements containing elements only. If e.g. line breaks are used in an attribute value or whitespace exist in an element containing elements only, the XML processor normalizes the document removing such whitespace.

The XML declaration must implicitly or explicitly use standalone="no" if the DTD effects the XML document with default values for attributes or "external markup declaration" (entity declarations), or if the DTD is needed to normalize attribute values or to remove ignorable whitespace from element content.

2. Default values and Entities

Default values for attributes we also know from XML Schema but many XML developers don't know what "external markup declarations" are all about, something specific to the use of DTDs.

In a DTD we can declare that an entity like &jt; should be replaced by "Jesper Tverskov" whenever encountered in the XML instance document. In the DTD the declaration looks like this:

  1. <!ENTITY jt "Jesper Tverskov">

In much the same way we can make up entities that can include XML markup or text placed in external files. Entities for external markup declarations and text are only possible when we use DTDs. We have nothing similar in XML Schema. [2]

3. When an XML document can "standalone"

If there are no default values for attributes declared in the DTD and no external entities are declared in the DTD, and if no normalization is needed, the XML document can "standalone", that is the DTD is only for validation.

Only if the XML document can stand alone in the "DTD" sense, could it be relevant to use the standalone pseudo-attribute with the value "yes".

Even if the XML document can stand alone, there is often no need to use the standalone pseudo-attribute. It depends on the situation, on the software, XML-processor and network involved.

If the DTD is small and the external DTD subset file (*.dtd) is on the same server, or if things look to work well and fast, we don't have a problem, and we don't need to use the standalone pseudo-attribute.

4. Standalone="yes"

If the XML document has a DTD and standalone="yes" is used, it is a signal to the XML processor that the DTD is only for validation. The DTD is not needed to look anything up or to normalize attribute values or to remove ignorable whitespace from element content.

This is a useful option to consider if things work slower than we would like them to do. If the DTD is huge or the network slow and the DTD is only used for validation, then and only then is it relevant to use the standalone pseudo-attribute.

If our XML document has an external DTD subset and can not stand alone, and the XML document loads slowly, we should probably reconsider our project. Why not remake our DTD to get rid of externally declared entities and of default values for attributes and why not normalize attribute values ourselves and remove ignorable whitespace from element content in order to make it possible to use standalone="yes" to make our document load faster?

5. What about XML Schema

We can not declare external entities in XML Schema or in RELAX NG for that matter. This is only an option in a DTD. But in XML Schema we can also have default values for attributes like in a DTD. So why does the standalone pseudo-attribute not apply also to XML Schema?

For a very obvious reason: DTD is defined in the XML Recommendation (standard). It is only natural that a pseudo-attribute in the XML declaration can deal with a DTD problem. The XML Recommendation on the other hand knows today nothing about XML Schema or other schema languages being independent recommendations or standards made years after the XML Recommendation.



"Version", "encoding" and "standalone" look like attributes and is doing a job similar to attributes, but only elements can have attributes. The order of real attributes are not important, and we can get to real attributes with XPath expressions like @*, etc.

The order of the pseudo-attributes is important on the other hand, their sequence must be "version", "encoding", "standalone" if they all exist, and in XPath 1.0 we have no expressions or functions that can read or detect if an XML declaration is present or the values of the pseudo-attributes.

In XPath 2.0 we can get to the pseudo-attributes, but it is not easy. See my article: Using unparsed-text() in XSLT 2.0 to test prolog.


A schema language like RELAX NG has even made a point out of this: A schema should only be for validation. It is not the job of a RELAX NG schema to change or modify the XML instance document supplying externally declared markup or text for entity references or default values for attributes.

Updated: 2011-07-27