XML canonicalization (C14N)

XML canonicalization (C14N) converts an XML document into one fixed byte form, so formatting differences never break a signature.

XML canonicalization, commonly abbreviated C14N, is the process of converting an XML document into one exact, standardised sequence of bytes, so that documents which differ only in formatting end up identical after canonicalization. The differences it removes include:

  • whitespace between elements and attributes
  • the order in which attributes appear
  • redundant or differently ordered namespace declarations
  • line endings
  • character encoding

XML allows the same logical content to be written in many different ways; a hash does not work that way, since it treats its input as a plain string of bytes and any difference at all produces a completely different result.

This gap matters for XML signature (XMLDSig) and XAdES: a signature is computed over a hash of the data, so if the exact bytes shift - because a different XML parser re-serialised the document, or a tool re-indented it - the signature would appear broken even though nothing meaningful changed. Canonicalization removes that fragility by fixing the byte-level representation before hashing, on both the signing side and the verifying side.

Three related standards cover this:

  • Canonical XML 1.0: the original W3C standard, defining the canonical form for a complete XML document or a well-formed subset of one.
  • Canonical XML 1.1: a later revision that corrects how inherited xml: attributes (such as xml:lang and xml:base) are handled when only part of a document is canonicalized.
  • Exclusive XML canonicalization: a variant for signing an XML fragment that lives inside, or will be moved into, a larger document, so that namespace declarations from the surrounding context do not leak into the canonical form and invalidate the signature later.

Note: Canonicalization only produces an internal byte sequence used to compute the hash; it does not rewrite the file that is stored, transmitted or displayed. A signed XML document can still be reformatted for reading without affecting its signature, provided the canonicalization step is redone correctly during validation.

Frequently asked questions

Back to glossary