Although XML declarations are optional, every XML document should have one. An XML declaration helps both human users and automated software identify the document as XML. It identifies the version of XML in use, specifies the character encoding, and can even help optimize the parsing. Most importantly, it's a crucial clue that what you're reading is in fact an XML document in environments where file type information is unavailable or unreliable. The following are all legal XML declarations. <?xml version="1.0"?> <?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="ISO-8859-1" standalone="no"?> <?xml version="1.0" standalone="yes"?> In general the XML declaration must be the first thing in the XML document. It cannot be preceded by any comments, processing instruction, or even white space. The only thing that may sometimes precede it is the optional byte order mark. The XML declaration is not a processing instruction, even though it looks like one. If you're processing an XML document through APIs like SAX, DOM, or JDOM, the methods invoked to read and write the XML declaration will not be the same methods invoked to read and write processing instructions. In many cases, including SAX2, DOM2, XOM, and JDOM, the information from the XML declaration may not be available at all. The parser will use it to determine how to read a document, but it will not report it to the client application. Each XML declaration has up to three attributes.
Like other attributes, these may be enclosed in single or double quotes, and any amount of white space may separate them from each other. Unlike other attributes, order matters. The version attribute must always come before the encoding attribute, which must always come before the standalone declaration. The version attribute is required. The encoding attribute and standalone declaration are optional. |