Document syntax
Prolog
Everything in a document before the root element's start tag: the XML declaration, any comments or processing instructions, and the document type declaration.
The prolog is optional in its entirety — a document that begins directly with its root element is perfectly well-formed. What is not optional is the order. If an XML declaration is present it must be the very first thing in the file, before any whitespace, comment or byte other than a byte order mark.
That ordering rule causes one of the most common failures in generated XML. A template that emits a blank line, a licence comment, or a stray newline before the declaration produces a document no conforming parser will read, and the message names the declaration rather than the whitespace that displaced it.
- Defined in
- XML 1.0 §2.8
- Category
- Document syntax
- Also written
- document prolog, XML header
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.
A prolog is optional: a document may begin with its root element.
document.xml <greeting>hello</greeting>What the engine returns
Well-formed.But if a declaration is present, nothing may come before it — not even a newline.
document.xml <?xml version="1.0" encoding="UTF-8"?> <greeting>hello</greeting>What the engine returns
XML declaration allowed only at the start of the document (line 2)
What catches people out
- A leading blank line is invisible in most editors and fatal to the parser. If your build concatenates files, the newline between them lands in exactly the wrong place.
- Comments and processing instructions are legal in the prolog, but only after the XML declaration.
- The prolog ends where the root element's start tag begins. There is no marker for it.
Where it bites
A definition on its own is trivia. These are the pages where this term decides whether a document works.
- Parse errorsXML declaration allowed only at the start of the documentThe message a parser prints when something got in front of the declaration.
- Parse errorsDocument is emptyWhat you get instead when the prolog is all there is.
Related terms
- XML declarationThe optional <?xml version="1.0" encoding="UTF-8"?> at the very start of a document, declaring its version and character encoding.
- Document type declarationThe <!DOCTYPE root …> in the prolog that names the document's root element and points at, or contains, its markup declarations.
- Processing instructionA <?target data?> node carrying instructions for an application, passed through the parser without being interpreted as content.
- Byte order markThe optional U+FEFF character at the start of a file, marking the encoding and — in UTF-16 — its byte order.
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.