XML Prolog and Declaration
Estimated study time: 8 minutes.
Most XML documents begin with a special line called the prolog, which contains the XML declaration. It isn't a tag in the usual sense — it's metadata that tells the parser how to interpret the document.
The Declaration Line
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
Breaking It Down
- version — the XML specification version being used, almost always
1.0. - encoding — the character encoding of the document, commonly
UTF-8. - standalone — indicates whether the document relies on an external DTD (
"no") or is self-contained ("yes").
Placement Rules
- The declaration must be the very first line of the document — nothing, not even whitespace, can come before it.
- It's technically optional, but strongly recommended for portability and correct parsing.
What Goes in the Prolog Besides the Declaration?
The prolog can also include comments and a Document Type Declaration (DTD) reference, all placed before the root element.
<?xml version="1.0" encoding="UTF-8"?>
<!-- Course catalog data -->
<!DOCTYPE course SYSTEM "course.dtd">
<course>...</course>
💡 Tip: If your XML file will be read by systems expecting a specific encoding, always declare it explicitly rather than relying on the parser's default.