Skip to main content
XMLDir

Search 309 pages — tools, formats, elements, namespaces, directory, comparisons, XPath, datatypes, glossary, parse errors, schema errors, use cases, blog and product.

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&#233;</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.

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.