XML Declaration
An XML document should always begin with an XML declaration, also known as the prologue.
This special processing instruction provides the XML parser with essential metadata about the document itself, such as the XML version and the character encoding used.
What is the XML Declaration?
The XML declaration is a processing instruction that appears as the very first line of an XML file. It is not part of the XML document's tree structure (meaning it's not an element node), but instead serves as a header for the parser.
While it is technically optional in some cases, it is a universal best practice to always include it.
Example:
<?xml version="1.0" encoding="UTF-8"?>
<root>
...
</root>
Syntax and Attributes
The declaration is enclosed in <?xml ... ?> tags and contains up to three attributes.
<?xml
version = "version_number"
encoding = "encoding_declaration"
standalone = "standalone_status"
?>
Each parameter consists of a parameter name, an equals sign (=), and parameter value inside a quote.
| Attribute | Description | Common Values |
|---|---|---|
version | (Required) Specifies the version of the XML standard being used. | "1.0" is the most common and universally supported version. |
encoding | (Optional) Specifies the character encoding used to write the document. If omitted, the default is UTF-8. | "UTF-8" (recommended), "UTF-16", "ISO-8859-1" |
standalone | (Optional) Specifies whether the document has external markup declarations (like in an external DTD). | "yes" or "no". The default is "no". |
standalone="yes": Tells the parser that the document is self-contained and does not require fetching any external resources (like a DTD) to be parsed correctly.standalone="no": Tells the parser that the document may depend on an external declaration.
Key Rules for the XML Declaration
The syntax for the XML declaration is very strict and must be followed precisely. An XML declaration should respect the following rules:
- Must Be First: If it exists, it must be the very first thing in the file, with no preceding characters or whitespace.
- Begins with
<?xml: The tag must be lowercase. versionis Required: Theversionattribute is the only mandatory attribute.- Strict Order: The attributes must appear in the following order:
version,encoding,standalone. - Case-Sensitive: The attribute names (
version,encoding,standalone) must be lowercase. - No Closing Tag: The declaration is a self-contained processing instruction and does not have a closing tag like
</?xml>. - Both single and double quotes can be used.
Practical Examples
Some examples of XML declaration:
Minimal Declaration (Required)
This is the simplest valid XML declaration. The encoding will default to UTF-8.
<?xml version="1.0"?>
Declaration with Encoding (Most Common)
This is the most common and recommended declaration for new XML files.
<?xml version="1.0" encoding="UTF-8"?>
Full Declaration (declaration with all parameters defined) using Double Quotes
This declaration specifies that the document is self-contained and does not rely on external DTDs.
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
Full Declaration Using Single Quotes
Single quotes are also valid for attribute values.
<?xml version='1.0' encoding='UTF-8' standalone='yes'?>
Conclusion
The XML declaration is a simple but essential part of a well-formed XML document.
- Always include it as the first line of your XML files.
- At a minimum, specify the
version. - It is a strong best practice to also specify the
encoding, withUTF-8being the modern standard.
By starting your documents with a correct and complete XML declaration, you ensure that they can be reliably parsed by any standard XML processor.