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.

Parsing and processing

Canonicalization (C14N)

Rewriting a document into a single normal form — sorted attributes, expanded empty elements, resolved references, no declaration — so that equivalent documents become byte-identical.

Canonicalization exists because XML has many spellings for the same content, and a digital signature has to sign one of them. Sort the attributes, expand `<x/>` into `<x></x>`, replace CDATA with escaped text, normalise the namespace declarations, drop the XML declaration: what is left is comparable byte for byte.

Exclusive C14N differs in one important way — it omits namespace declarations the subtree does not actually use. That is what makes a signed fragment survive being moved into another document, which is why SOAP and WS-Security use it.

Defined in
Canonical XML 1.0 / 1.1 / Exclusive
Category
Parsing and processing
Also written
C14N, canonical XML

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.

  • Attributes are sorted, empty elements expanded, and the declaration dropped.

    document.xml
    <?xml version="1.0"?>
    <r z="26" a="1"><e/></r>

    What the engine returns

    c14n
    <r a="1" z="26"><e></e></r>
    
  • Inclusive C14N keeps a namespace declaration nothing uses; exclusive C14N drops it. Here is the inclusive form.

    document.xml
    <r xmlns:unused="urn:unused"><e/></r>

    What the engine returns

    c14n
    <r xmlns:unused="urn:unused"><e></e></r>
    
  • And here is the exclusive form of the same document.

    document.xml
    <r xmlns:unused="urn:unused"><e/></r>

    What the engine returns

    exc-c14n
    <r><e></e></r>
    

What catches people out

  • Canonicalization does not preserve CDATA sections. A signature over a canonical form is unaffected; a naive byte comparison against the original is not.
  • Whitespace between elements is content and is kept. Canonical form is not minified form.
  • Choosing inclusive C14N for a fragment that will be embedded elsewhere is how signatures break on relocation. That is what exclusive C14N is for.

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.