Document syntax
XML declaration
The optional <?xml version="1.0" encoding="UTF-8"?> at the very start of a document, declaring its version and character encoding.
It looks like a processing instruction and is not one: the specification defines it separately, and an XPath expression counting processing instructions will not count it. Its three pseudo-attributes — version, encoding and standalone — must appear in that order, and version is required whenever the declaration is present at all.
The encoding pseudo-attribute is a statement about the bytes, not an instruction to convert them. Declaring UTF-16 over UTF-8 bytes transcodes nothing; it makes the parser read the file through the wrong decoder, and the failure surfaces as a syntax error inside the declaration itself.
- Defined in
- XML 1.0 §2.8
- Category
- Document syntax
- Also written
- <?xml ?>, XML prolog declaration
Shown, not asserted
All 2 claims are executed on every test run, against the same engines the tools use. If the behaviour changes, the test fails rather than this page quietly becoming wrong.
The declaration is not a processing instruction: this document has one PI, not two.
document.xml <?xml version="1.0"?> <?xml-stylesheet href="s.xsl"?> <x>hi</x>expression count(//processing-instruction())What the engine returns
1Declaring an encoding the bytes are not breaks parsing rather than converting anything.
document.xml <?xml version="1.0" encoding="UTF-16"?> <x>hi</x>What the engine returns
Blank needed here (line 1)
What catches people out
- It is optional. A document without one is UTF-8 (or UTF-16 with a byte order mark) by definition, and is perfectly valid.
- The pseudo-attributes are order-sensitive: version, then encoding, then standalone. Reversing the first two is an error, not a style choice.
- standalone="yes" promises that no external markup declarations affect the document's content. It is almost always either wrong or pointless.
Where it bites
A definition on its own is trivia. These are the pages where this term decides whether a document works.
- Parse errorsMalformed declaration expecting versionWhat a declaration missing its version, or with pseudo-attributes out of order, reports.
- Parse errorsUnsupported encodingThe failure when the declared encoding names something the parser has no decoder for.
- XPathcount()Counting processing instructions is how you can see the declaration is not one.
Related terms
- PrologEverything in a document before the root element's start tag: the XML declaration, any comments or processing instructions, and the document type declaration.
- Encoding declarationThe encoding pseudo-attribute in the XML declaration, telling the parser which character encoding the file's bytes are in.
- Processing instructionA <?target data?> node carrying instructions for an application, passed through the parser without being interpreted as content.
- UTF-8The default encoding for XML, and the one every conforming parser must support along with UTF-16.
- XML 1.1A revision of XML that widens the legal name characters and permits most control characters as escapes — almost never used, and best avoided.
Get started
Bring order to the XML your team can't afford to ignore.
Create a free account and get a private workspace to search, validate, diff, and monitor your XML feeds, sitemaps, schemas, and vendor integrations.