XML technologies / XML / XML in a Nutshell / XML basics / The XML declaration
(Excerpt from “XML in a Nutshell” by Elliotte Rusty Harold & W. Scott Means)
XML documents should, but need not, begin with an XML declaration. It resembles a processing instruction named xml with pseudo-attributes version, standalone and encoding. Technically, however, it is not a processing instruction, just an XML declaration—nothing more or less. The following example illustrates this.
<?xml version="1.0" encoding="US-ASCII" standalone="yes"?>
<person>
Alan Turing
</person>Code example: A very simple XML document with an XML declaration
XML documents need no XML declaration. If present, however, it must be the first thing in the document. No comments, whitespace or processing instructions may precede it. The XML parser uses the first five characters (<?xml) to make informed guesses about encoding, such as whether characters use one or multiple bytes. Only an invisible Unicode byte-order mark may precede the declaration. We discuss this in Internationalization.
The version attribute
The attribute version should have the value 1.0. In very unusual circumstances, it may instead be 1.1. Since choosing version="1.1" restricts the document to the newest versions of a few parsers, and every XML 1.1 parser must also support XML 1.0, do not casually select version 1.1.
Not convinced? First answer these questions:
Do you speak Cambodian, Burmese, Amharic, Mongolian or Dhivehi?
Does your data contain obsolete, nontext C0 control characters such as vertical tab, line feed or BELL?
If both answers are no, XML 1.1 offers no advantage. If either or both are yes, you may have a reason to use it. XML 1.0 permits Cambodian, Burmese, Amharic and similar scripts in character data and attribute values; XML 1.1 also permits them in element and attribute names, unlike XML 1.0. XML 1.1 also allows C0 controls except NUL in character data and attribute values, provided they are escaped as numeric character references such as . XML 1.0 does not permit that either. If either condition applies, consider XML 1.1, while remembering that it limits your potential audience. Otherwise, stick exclusively to XML 1.0.
The encoding attribute
So far we have been careless about character sets and encodings. We said XML is plain text, without identifying its encoding. ASCII? Latin-1? Unicode? Something else?
The short answer is yes. The longer answer is that XML documents are assumed by default to use the variable-length UTF-8 encoding of Unicode. It is a proper superset of ASCII, so plain ASCII files are also UTF-8 documents. Most XML processors, especially those written in Java, can handle many more character sets. Just tell the parser the document’s encoding. Preferably this is metadata stored in the filesystem or supplied by the server. Since not all systems provide character-set metadata, XML lets a document specify its own character set using an encoding declaration (English: encoding declaration) in the XML declaration. The next example indicates a document written in ISO-8859-1 (Latin-1), which includes characters such as ö and ç required by many non-English Western European languages.
<?xml version="1.0" encoding="ISO-8859-1" standalone="yes"?>
<person>
Erwin Schrödinger
</person>Code example: An XML document encoded in Latin-1
The attribute encoding is optional. If omitted and no metadata exists, Unicode is assumed. The parser can examine the first bytes to guess the Unicode encoding. If metadata conflicts with the declaration, the metadata encoding wins. For example, if an HTTP header says ASCII while the declaration says UTF-8, the parser chooses ASCII.
Different encodings and correct handling of non-English XML documents are explained in detail in Internationalization.
The standalone attribute
If the attribute standalone has the value no, an application may need an external DTD —a DTD in another file—to obtain correct values for document parts. A DTD might supply default attribute values that the parser must report even though absent from the current document.
Documents without DTDs, like all examples in this section, may set yes as the value of standalone. Documents with DTDs can also set the attribute standalone to yes if the DTD changes no content or is entirely internal. Details about documents with DTDs appear in Document type definitions (DTDs).
The attribute standalone is optional in an XML declaration. If omitted, the assumed value is no.
Copyright © 2005 O'Reilly Verlag GmbH & Co. KG
You may print the online version for your private use.
Otherwise, this chapter from “XML in a Nutshell” is subject to the same conditions as the bound edition: the work, including all its parts, is protected by copyright. All rights reserved, including reproduction, translation, microfilming, and storage and processing in electronic systems.
O’Reilly Verlag GmbH & Co. KG, Balthasarstraße 81, 50670 Köln, kommentar(at)oreilly.de