SyntaxStudy
Sign Up
XML The XML Prolog and Declaration
XML Beginner 1 min read

The XML Prolog and Declaration

Every XML document may begin with an XML declaration, a processing instruction that tells parsers how to interpret the document. Although optional, it is strongly recommended. The declaration takes the form and must appear on the very first line with no preceding whitespace or byte-order mark issues. The version attribute currently has only one defined value: "1.0". There is an XML 1.1 specification, but it never saw wide adoption, so 1.0 is universally used. The encoding attribute specifies the character encoding of the document. UTF-8 is the most common choice because it covers all Unicode characters while remaining ASCII-compatible. UTF-16 is also allowed but less common on the web. If the encoding attribute is omitted, UTF-8 or UTF-16 are assumed based on the byte-order mark. After the declaration, a document may include a DOCTYPE declaration referencing a DTD, followed by comments and processing instructions — all of which form the document prolog. The prolog precedes the single required root element. Processing instructions like are also part of the prolog and instruct applications to process the document in a specific way.
Example
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<!--
    XML Declaration breakdown:
      version    = "1.0"   (required)
      encoding   = "UTF-8" (optional, defaults to UTF-8/UTF-16)
      standalone = "yes"   (optional: "yes" means no external DTD needed)
-->

<!-- Processing instruction: attach an XSL stylesheet -->
<?xml-stylesheet type="text/xsl" href="books.xsl"?>

<!-- DOCTYPE declaration referencing an external DTD -->
<!DOCTYPE bookstore SYSTEM "bookstore.dtd">

<!-- Root element — only one allowed per document -->
<bookstore>
    <!-- Character data (text node) -->
    <title>XML Fundamentals</title>

    <!-- CDATA section: content not parsed by the XML parser -->
    <description><![CDATA[
        This book covers <XML> & related technologies.
        Special characters like < > & need no escaping here.
    ]]></description>

    <!-- Entity references for reserved characters -->
    <note>Use &lt; for less-than and &amp; for ampersand.</note>
</bookstore>