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.

Document syntax

Character reference

A &#nnn; or &#xhhhh; escape naming a Unicode code point directly, rather than by an entity name.

Character references are resolved by the parser into the character they name, before anything downstream sees the document. They need no declaration — unlike entity references, they are part of the language — so they work in any document, including one with no DOCTYPE.

They are the escape hatch for characters your encoding cannot carry, and for the ones your tooling mangles. They also differ from literal characters in one place that matters: attribute-value normalization replaces a literal tab with a space, but leaves 	 alone.

Defined in
XML 1.0 §4.1
Category
Document syntax
Also written
&, &, numeric entity

Shown, not asserted

All 3 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.

  • A decimal reference resolves to its character.

    document.xml
    <x>caf&#233;</x>
    expression
    string(/x)

    What the engine returns

    café
  • The hexadecimal form names the same code point.

    document.xml
    <x>caf&#xE9;</x>
    expression
    string(/x)

    What the engine returns

    café
  • A reference to a code point XML forbids is an error, not a substitution.

    document.xml
    <x>a&#1;b</x>

    What the engine returns

    xmlParseCharRef: invalid xmlChar value 1 (line 1)

What catches people out

  • The hexadecimal form needs the x: &#x26; is an ampersand, &#26; is a control character and illegal.
  • A character reference resolves before validation, so a pattern facet sees the resolved character rather than the escape.
  • Referencing a character XML 1.0 forbids is fatal even though the escape itself looks well-formed.

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.