Inside a XML File: Signature, Structure and MIME Type
Learn how XML is identified and what its internal components mean.
The Binary Signature That Identifies XML
Every XML file announces itself through a text-based preamble that must appear at the very start of the file. Unlike binary formats that rely on fixed hexadecimal sequences—PNG’s 89 50 4E 47, for instance—XML’s signature is the string <?xml, corresponding to bytes 3C 3F 78 6D 6C. This is the opening of the XML declaration, and its presence at offset zero provides the most reliable programmatic identification of an XML file.
The W3C XML 1.0 specification (Section 2.8) defines this as the beginning of the prolog: “An XML entity which begins with the XML declaration is a prolog.” A Byte Order Mark (BOM) may precede the declaration when the file uses UTF-16 or UTF-32 encoding. In UTF-16, the BOM bytes FE FF or FF FE appear before <?xml; in UTF-8, the BOM EF BB BF is permitted but uncommon. A parser must handle the BOM transparently and still require the declaration for the file to be treated as XML with explicit version and encoding metadata.
Unix file command behavior reflects this distinction. An XML file beginning with <?xml version="1.0" returns XML 1.0 document text. A file that omits the declaration and opens directly with a root element may return ASCII text or UTF-8 Unicode text, forcing the parser to infer XML status from the presence of a root element. This inference is heuristic and unreliable across tools. For deterministic identification, verify that <?xml begins at byte zero, or at byte two or three if a BOM is present.
The Prolog: Version, Encoding, and the Standalone Flag
The prolog is metadata about the document, structurally distinct from the root element that follows it. Its primary component is the XML declaration:
<?xml version="1.0" encoding="UTF-8"?>
version
The version attribute accepts exactly two values: "1.0" and "1.1". XML 1.0, published in 1998 and revised through five editions, remains the baseline for interoperability. XML 1.1, published in 2004, relaxes character restrictions to permit Unicode characters introduced after XML 1.0, particularly in element and attribute names. However, XML 1.1 breaks backward compatibility: a 1.0 parser must reject a 1.1 document. If the declaration is absent, parsers default to version 1.0.
encoding
The encoding attribute declares the character encoding of the byte stream. XML 1.0 Section 4.3.3 specifies UTF-8 as the default when no encoding is declared and no BOM is present. Other valid values include UTF-16, ISO-8859-1, Shift_JIS, and any character set registered with IANA. The declared encoding must match the actual bytes on disk. A file saved as UTF-16 but declaring encoding="UTF-8" produces a fatal parse error because the byte sequences for characters like < differ between encodings. Parsers detect this mismatch by examining the first few bytes against the declaration and halt immediately.
standalone
The optional standalone attribute takes "yes" or "no". A value of "yes" asserts that the document contains no external markup declarations—no references to an external DTD that would supply entity definitions, attribute defaults, or element content models. standalone="no" or omission signals that external declarations may exist. This flag affects DTD-driven validation but not well-formedness. A document declaring standalone="yes" that nonetheless references an external DTD is invalid but may still be well-formed.
The prolog may also contain processing instructions targeting specific applications, such as <?xml-stylesheet type="text/xsl" href="transform.xsl"?>. These instructions are not defined by the XML specification itself but are widely used to associate XSLT stylesheets with XML documents.
The Body: Elements, Attributes, and the Single Root Requirement
The W3C specification (Section 3) defines an XML document as a sequence of characters conforming to the document production, which requires exactly one root element enclosing all other content. This constraint is absolute: a file with two sibling top-level elements is not well-formed and will be rejected by any conforming parser.
A minimal well-formed XML document:
<?xml version="1.0" encoding="UTF-8"?>
<library>
<book id="b001">
<title>XML Explained</title>
<author>R. Taggart</author>
</book>
</library>
Elements
Each element consists of an opening tag and a closing tag with identical names. Tags are case-sensitive: <Book> and </book> do not match and produce a well-formedness error. Empty elements may use self-closing syntax: <isbn number="978-0-123-45678-9" />. The space before the slash is conventional but not required.
Attributes
Attributes appear within opening tags as name-value pairs. Values must be enclosed in single or double quotes; unquoted attributes are forbidden. Attribute names within a single element must be unique—<book id="1" id="2"> is not well-formed. The characters < and & must be escaped as < and & within attribute values.
Nesting
Elements must nest completely. <book><title>...</title></book> is valid. <book><title>...</book></title> produces a fatal “not well-formed” error because the closing tags cross. Parsers are required to report this error and cease processing; no recovery is permitted.
Character Data and CDATA Sections
Text content containing markup characters can be wrapped in a CDATA section: <![CDATA[<greeting>Hello</greeting>]]>. The parser treats everything between <![CDATA[ and ]]> as literal text. CDATA sections cannot nest, and the sequence ]]> cannot appear within them.
The Epilogue: Content After the Root Element
After the closing tag of the root element, an XML file may contain:
- Comments:
<!-- comment -->. Comments cannot appear inside tags or CDATA sections and must not contain the sequence--. - Processing instructions:
<?target data?>for application-specific directives. - Whitespace: Spaces, tabs, and newlines are permitted and typically ignored.
Any non-whitespace content after the root element that is not a comment or processing instruction constitutes a well-formedness error. A parser encountering such content must report an error. There is no formal end-of-file marker; the parser stops when it encounters the root element’s closing tag and verifies that only permitted trailing constructs remain.
MIME Type and File Extension Conventions
IANA registers application/xml as the official MIME type for XML. RFC 7303, which obsoletes RFC 3023, clarifies the media type registration and deprecates text/xml for generic XML due to charset handling inconsistencies. The text/xml type defaults to us-ascii unless overridden, while application/xml defaults to the encoding specified in the document’s declaration or BOM.
Common XML-based MIME types:
| MIME Type | Format |
|---|---|
application/xml |
Generic XML |
application/rss+xml |
RSS feeds |
application/atom+xml |
Atom feeds |
application/xhtml+xml| XHTML |
|
application/svg+xml |
SVG vector graphics |
application/xslt+xml |
XSLT stylesheets |
File extensions are not standardized by the W3C. .xml is the universal default. Vocabulary-specific extensions—.xsl, .xsd, .svg, .rss—signal the expected XML application. Web servers should serve .xml files with Content-Type: application/xml. Using text/xml risks incorrect charset interpretation by clients that apply the us-ascii default.
Validation Checks and Hard Limits
Three layers determine whether an XML file is processable:
Well-formedness: The file must satisfy the XML grammar. A single root element, proper nesting, quoted attributes, matching case on tags, and correct escaping of
&,<,>,', and"are all required. Any violation is a fatal error. The parser must stop and report the error; it cannot guess the intended structure.Encoding agreement: The declared encoding must match the byte stream. A mismatch is fatal and detected early, typically within the first few bytes. A file with a UTF-16 BOM but an
encoding="UTF-8"declaration will fail immediately.Validity (optional): A well-formed document may be validated against a DTD, XML Schema (XSD), or RELAX NG schema. Validation failures are not fatal to parsing but may be treated as errors by the consuming application. Many production systems accept well-formed XML without validation.
A practical boundary: a file beginning with <?xml version="1.0" encoding="UTF-8"?> and containing a single root element with properly nested children is unambiguously XML. A file lacking the declaration but containing a single root element may be XML, but parsers configured to require a prolog will reject it. The safest practice is always to include the XML declaration at the start of the file, preceded only by an optional BOM.
Reference for inside a xml file structure and signature
W3C XML 1.0 Specification, Fifth Edition: https://www.w3.org/TR/xml/