XML Prolog

The prolog is the very beginning of an XML document, appearing before the root element. It provides essential information about the document itself — which version of XML it uses, what character encoding it is stored in, and whether it depends on external files. A parser reads the prolog first and uses that information before processing any of the actual data.

Think of the prolog like the cover page of a book. Before you read the content, the cover tells you the edition, language, and publisher. The prolog does the same for an XML parser.

What the Prolog Contains

The prolog can contain three things, in this order:

  1. The XML declaration (optional but strongly recommended)
  2. Processing instructions (optional)
  3. Comments (optional)

The prolog ends where the root element begins.

The XML Declaration

The XML declaration is the most important part of the prolog. It identifies the file as an XML document and specifies the version and encoding.

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

XML Declaration Attributes

AttributeRequiredValuesMeaning
versionYes (if declaration present)"1.0" or "1.1"XML version used
encodingNo (defaults to UTF-8)"UTF-8", "UTF-16", "ISO-8859-1", etc.Character encoding of the file
standaloneNo (defaults to "no")"yes" or "no"Whether the file depends on external DTD

Declaration with All Three Attributes

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>

The version Attribute

Nearly every XML file in production uses version 1.0. XML 1.1 exists but is rarely used — it supports some additional Unicode characters that 1.0 does not allow in element names. Unless you have a specific reason to use 1.1, always write version="1.0".

The encoding Attribute

Encoding tells the parser how to convert the bytes in the file into readable characters. The most important rule is that the declared encoding must match the actual encoding the file is saved in. A mismatch produces garbled text.

<?xml version="1.0" encoding="UTF-8"?>     <!-- most common -->
<?xml version="1.0" encoding="UTF-16"?>    <!-- for East Asian content -->
<?xml version="1.0" encoding="ISO-8859-1"?> <!-- legacy Western European -->

If no encoding is declared, the parser defaults to UTF-8 (or UTF-16 if it detects a byte order mark). UTF-8 covers every character in every language, so it is the recommended choice for all new XML files.

The standalone Attribute

Standalone tells the parser whether external DTD files contain declarations that affect the document's content. Most projects leave this out entirely.

standalone="yes"   → document is self-contained, no external DTD affects it
standalone="no"    → document may rely on an external DTD (default)

Processing Instructions in the Prolog

Processing instructions can appear in the prolog to provide instructions for specific applications. The most common one links a stylesheet to the XML file.

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/css" href="style.css"?>
<catalog>
  ...
</catalog>

Comments in the Prolog

Comments can appear in the prolog to document the file's purpose, author, or date. They cannot appear before the XML declaration.

<?xml version="1.0" encoding="UTF-8"?>
<!-- Product catalog — updated March 2024 -->
<!-- Author: Meena Krishnan -->
<catalog>
  ...
</catalog>

Full Prolog Example

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE catalog SYSTEM "catalog.dtd">
<?xml-stylesheet type="text/xsl" href="catalog.xsl"?>
<!-- Inventory catalog for the Bangalore warehouse -->
<catalog>
  <product id="P001">
    <name>USB Hub</name>
    <price>1299</price>
  </product>
</catalog>

Prolog Structure Diagram

┌─────────────────────────────────────────────────┐
│  PROLOG                                         │
│                                                 │
│  <?xml version="1.0" encoding="UTF-8"?>         │← XML Declaration
│  <!DOCTYPE catalog SYSTEM "catalog.dtd">        │← DTD Reference
│  <?xml-stylesheet type="text/xsl" href="..."?>  │← Processing Instruction
│  <!-- Author: Meena -->                         │← Comment
│                                                 │
└─────────────────────────────────────────────────┘
         ↓
<catalog>   ← Root element begins — prolog ends here
  ...
</catalog>

Rules for the XML Declaration

  • If present, it must be the very first line of the file — no blank lines, no spaces, no BOM before it.
  • The attributes must appear in this exact order: version, then encoding, then standalone.
  • It is not technically mandatory, but omitting it means the parser assumes version 1.0 and UTF-8 encoding.
  • It uses the processing instruction syntax <?...?> but is not a real processing instruction — it is special syntax reserved for the XML declaration only.

Key Points to Remember

  • The prolog appears at the very start of an XML document, before the root element.
  • The XML declaration specifies version, encoding, and optionally standalone.
  • UTF-8 is the recommended encoding for nearly all XML files.
  • The declared encoding must match the actual file encoding on disk.
  • Processing instructions and comments may also appear in the prolog.
  • The XML declaration must be the absolute first thing in the file if it is present.

Leave a Comment

Your email address will not be published. Required fields are marked *