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.

What Happens When the Declaration Is Wrong

Parsers are strict about the declaration because they read it before anything else to decide how to decode the bytes. Three problems appear again and again.

1. Something before the declaration

A blank line, a space or a comment above <?xml ...?> causes an error. Parsers report messages similar to "XML declaration allowed only at the start of the document". This often happens when a script prints a newline before writing the XML, or when files are concatenated.

2. Declared encoding does not match the real bytes

If a file says encoding="UTF-8" but was saved in a Windows code page, characters like the rupee sign or accented letters turn into errors or garbage. Save the file in the encoding you declare, and check your editor's status bar to confirm.

3. Wrong spelling or quotes

The attributes must appear in a fixed order: version, then encoding, then standalone. Names are lowercase, and values need matching single or double quotes.

<!-- invalid: encoding before version -->
<?xml encoding="UTF-8" version="1.0"?>

<!-- valid -->
<?xml version="1.0" encoding="UTF-8"?>

The Byte Order Mark and Encodings

Some editors add a byte order mark (BOM), a few invisible bytes, at the very start of UTF-8 or UTF-16 files. XML parsers are expected to handle a BOM, and it does not count as "something before the declaration". Problems usually arise in tools that are not true XML parsers, such as simple text scripts that look for <?xml as the first characters. When in doubt, save UTF-8 without a BOM.

A document with no encoding declaration and no BOM must be UTF-8. That is why many files work even when the declaration is missing, and also why a file in another encoding must always declare it.

Writing the Declaration from Code

Let your library write the prolog instead of building the string by hand. In Python, ElementTree adds a correct declaration when asked:

import xml.etree.ElementTree as ET

root = ET.Element("catalog")
item = ET.SubElement(root, "item", id="101")
item.text = "Notebook"

tree = ET.ElementTree(root)
tree.write("catalog.xml", encoding="UTF-8", xml_declaration=True)

The library both declares UTF-8 and encodes the bytes as UTF-8, so the two never disagree. Java's Transformer, .NET's XmlWriter and similar tools offer the same option.

Quick Prolog Checklist

  • The declaration is the first thing in the file, with nothing before it except an optional BOM.
  • Use version="1.0" unless a system specifically requires 1.1.
  • The declared encoding matches how the file is actually saved.
  • Set standalone="yes" only when no external DTD affects the content; if unsure, leave it out.
  • Comments and the DOCTYPE go after the declaration and before the root element.

Ready to go beyond the basics?

Get hands-on training, live mentorship, and placement support with the Data Analytics Course at Uncodemy.

Explore XML Course →