Skip to main content
XMLDir

Search 309 pages — tools, formats, elements, namespaces, directory, comparisons, XPath, datatypes, glossary, parse errors, schema errors, use cases, blog and product.

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

    1
  • Declaring 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.

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.