The XML declaration is not required in XML 1.0, as long as the file is encoded in UTF-8 or UTF-16. It becomes required in two cases: the document is XML 1.1, or the file uses another encoding and nothing outside the file says which one. If you do write the declaration, it must be the very first thing in the file.
When is the XML declaration required?
The XML declaration is required for every XML 1.1 document, and for XML 1.0 documents saved in an encoding other than UTF-8 or UTF-16. In every other case the line <?xml version="1.0"?> is optional.
| Your document | Declaration needed? | Why |
|---|---|---|
| XML 1.0, UTF-8 or plain ASCII | No | UTF-8 is the assumed encoding |
| XML 1.0, UTF-16 with a byte order mark | No | The byte order mark identifies the encoding |
| XML 1.0, another encoding such as ISO-8859-1 | Yes, with encoding |
The parser can't guess the encoding |
| XML 1.0, another encoding, charset sent by HTTP | No | The transport already names the encoding |
| XML 1.1 | Yes | The 1.1 rules say MUST |
The XML 1.0 specification says documents "SHOULD begin with an XML declaration". In standards language, SHOULD means recommended, not mandatory. The grammar agrees: the prolog rule lists the declaration with a question mark after it, which marks an optional part.
The XML 1.1 specification changes one word. There, documents "MUST begin with an XML declaration", and the question mark is gone from the grammar. So a file with no declaration is always read as XML 1.0, the only version that can omit it.
New to the format? Start with what an XML file is.
What the XML declaration contains
The XML declaration contains up to three settings in a fixed order: version, then encoding, then standalone. Only version is mandatory once you write the declaration at all.
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
version names the XML version, and for XML 1.0 documents the spec says not to use any number other than 1.0. encoding names the character encoding of the bytes in the file.
standalone says whether external markup declarations can change the content. With yes, nothing outside the file affects what the parser hands to your program. Leave it out and, when external declarations exist, no is assumed. For most documents it does nothing: the spec says that with no external markup declarations, the setting "has no meaning". I leave it out unless a schema or partner system asks for it.
The declaration looks like an element, but as one XML introduction puts it, the declaration is not a tag. It has no closing tag and it doesn't appear in the document tree.
When the encoding declaration matters
The encoding part of the XML declaration matters only when the file is not UTF-8 or UTF-16. A parser reads a file with no byte order mark and no encoding declaration as UTF-8, and plain ASCII is a subset of UTF-8, so ASCII files pass too.
Anything else has to be labelled. The 1.0 spec makes it a fatal error for a file with neither a byte order mark nor an encoding declaration to use an encoding other than UTF-8, unless a transport protocol such as HTTP supplies the encoding.
I tested this with Python 3.14, whose built-in parser is Expat. I saved <a>café</a> as Latin-1 bytes with no declaration, and parsing failed:
not well-formed (invalid token): line 1, column 6
Column 6 is the é, whose single Latin-1 byte is not legal UTF-8. Adding <?xml version="1.0" encoding="ISO-8859-1"?> in front fixed it, and the text came back as café.
The reverse mistake is also fatal: if the declaration names one encoding and the bytes are in another, the parser must stop. That "invalid token" message has other causes too, listed in the guide to the XML parsing error "not well-formed".
What breaks when the XML declaration is in the wrong place
An XML declaration that isn't at the very start of the file is a fatal error. Only a byte order mark may come before it. A space, a blank line or a comment in front all fail.
Expat, through Python, gave me these:
| Input starts with | Error |
|---|---|
A space, then <?xml version="1.0"?> |
XML or text declaration not at start of entity: line 1, column 1 |
<!-- c --> then the declaration |
XML or text declaration not at start of entity: line 1, column 10 |
<?xml encoding="UTF-8"?> (no version) |
XML declaration not well-formed: line 1, column 6 |
standalone written before encoding |
XML declaration not well-formed: line 1, column 37 |
Both strings come straight from the Expat source. The libxml2 parser words the first case differently in its source: XML declaration allowed only at the start of the document.
The usual cause is a template or script that prints a blank line before the XML starts. Two files joined together do it too, because the second file's declaration lands in the middle. Paste a suspect file into the XML viewer and look at what sits before <?xml.
Do tools write the XML declaration by default?
Many XML writers skip the XML declaration by default, so don't assume your output has one. Python's standard library is a good example.
In ElementTree, tostring() takes an xml_declaration option: True always writes it, False never does, and the default None writes it only when the encoding isn't US-ASCII, UTF-8 or Unicode. The option was added to tostring() in version 3.8.
import xml.etree.ElementTree as ET
root = ET.fromstring("<a>x</a>")
ET.tostring(root)
# b'<a>x</a>'
ET.tostring(root, xml_declaration=True)
# b"<?xml version='1.0' encoding='us-ascii'?>\n<a>x</a>"
A round trip through a parser can drop the declaration your input had. That's legal for UTF-8 output, but it surprises people who compare the files byte by byte. When you convert XML to JSON, the declaration has no place in the result and is left out.
Should you include the XML declaration anyway?
Yes, include the XML declaration in files you write, even when the rules let you skip it. My take: <?xml version="1.0" encoding="UTF-8"?> costs one line and tells every reader, human or parser, what they're holding.
The exception is XML embedded inside another document. A fragment pasted into a larger file can't carry its own declaration, because the declaration would no longer be at the start. Strip it before you embed. Mixed vocabularies in one file are a job for XML namespaces, not for extra declarations.
The limitation: the declaration is only a label and converts nothing. If your editor saves the file as Windows-1252 while the first line says UTF-8, the parser fails on the first accented character. Check the editor's save encoding too.
FAQ
Is an XML file valid without the XML declaration?
Yes, in XML 1.0. A file with no declaration is well-formed as long as it is encoded in UTF-8 or UTF-16. A parser treats it as an XML 1.0 document.
Can I write the XML declaration without the version?
No. Inside the declaration, version is mandatory and has to come first. Encoding and standalone are optional, and they must follow in that order.
Can anything come before the XML declaration?
Only a byte order mark. A space, a blank line or a comment in front of the declaration is a fatal error in conforming parsers.
Is the encoding attribute required in the XML declaration?
Only when the file is not UTF-8 or UTF-16 and nothing outside the file, such as an HTTP header, names the encoding. Plain ASCII files count as UTF-8.
What does standalone do in the XML declaration?
standalone="yes" tells the parser that no external markup declarations change the document's content. If the document has no external DTD, the setting has no meaning.