Encoding and characters
UTF-8
The default encoding for XML, and the one every conforming parser must support along with UTF-16.
A document with no encoding declaration and no byte order mark is UTF-8 by definition. Everything else — Latin-1, Windows-1252, Shift-JIS — has to be declared, and support for it is a property of your parser rather than of the specification.
Because UTF-8 can represent every character, encoding is never a reason to escape one. Character references remain useful for characters your toolchain mangles or your editor cannot show, not for characters the encoding cannot carry.
- Defined in
- XML 1.0 §4.3.3
- Category
- Encoding and characters
- Also written
- Unicode, charset
Shown, not asserted
All 2 claims are executed on every test run, against the same engines the tools use. If the behaviour changes, the test fails rather than this page quietly becoming wrong.
Non-ASCII characters need no declaration and no escaping.
document.xml <x>café — 日本語 — 😀</x>What the engine returns
Well-formed.A character reference and the literal character are the same document, which canonical form makes obvious.
document.xml <x>café</x>What the engine returns
c14n <x>café</x>
What catches people out
- "UTF-8" and "unicode" are not synonyms, and a declaration of encoding="unicode" is not portable.
- Byte length and character count differ. Any limit expressed in characters needs care when the bytes are what is measured.
- Not every Unicode character is legal in XML 1.0, however well UTF-8 encodes it — the control characters remain forbidden.
Where it bites
A definition on its own is trivia. These are the pages where this term decides whether a document works.
- Parse errorsUnsupported encodingWhat declaring an encoding the parser lacks reports.
- ToolsXML canonicalizer (C14N)Resolves references to literal characters, as above.
Related terms
- Encoding declarationThe encoding pseudo-attribute in the XML declaration, telling the parser which character encoding the file's bytes are in.
- Byte order markThe optional U+FEFF character at the start of a file, marking the encoding and — in UTF-16 — its byte order.
- Character referenceA &#nnn; or &#xhhhh; escape naming a Unicode code point directly, rather than by an entity name.
- Invalid XML characterA code point XML forbids in a document at all — most of the C0 control range — which cannot be included literally or as an escape.
Get started
Bring order to the XML your team can't afford to ignore.
Create a free account and get a private workspace to search, validate, diff, and monitor your XML feeds, sitemaps, schemas, and vendor integrations.