Comparison
Canonical XML vs minified XML
Both rewrite a document without changing what it says. One makes two equivalent documents byte-identical; the other just makes them smaller. Confusing them breaks signatures.
Minifying strips the whitespace between elements and leaves everything else alone: the declaration stays, attribute order stays, an empty element stays self-closing. It is a size optimisation and nothing more.
Canonicalization normalises instead. Attributes are sorted, empty elements are expanded, CDATA becomes escaped text, the XML declaration is dropped, and namespace declarations are put in a defined order — so two documents that mean the same thing produce the same bytes. That is what a digital signature is computed over.
The trap is reaching for the wrong one. Minified output is not canonical, so signing it and comparing against a canonical form fails; and canonical output is not minified — it keeps the whitespace between elements, because whitespace is content until a schema says otherwise.
What actually differs
| Aspect | Canonical (C14N) | Minified |
|---|---|---|
| Purpose | Make equivalent documents byte-identical | Make the document smaller |
| XML declaration | Removed | Kept |
| Attribute order | Sorted | Left as written |
| Empty elements | Expanded to a start and end tag | Left as written |
| Whitespace between elements | Kept — it is content | Removed where it is safe to |
| CDATA | Replaced with escaped text | Kept |
| Use it for | Signatures, comparison, deduplication | Transfer size |
Shown, not asserted
The differences above are claims, so here they are being made. Every one of these runs on each test run, against the same engines the tools use — the outputs are what came back, not what we expected.
Canonical form sorts the attributes, expands the empty element and drops the declaration.
document.xml <?xml version="1.0"?> <r z="26" a="1"><e/></r>What came back
c14n <r a="1" z="26"><e></e></r>Minifying the same document changes none of that — it only removes whitespace.
document.xml <?xml version="1.0"?> <r z="26" a="1"><e/></r>What came back
minify <?xml version="1.0"?> <r z="26" a="1"><e/></r>And canonical form keeps the whitespace between elements, which is exactly why it is not a minifier.
document.xml <r> <a/> </r>What came back
c14n <r> <a></a> </r>
Which to pick
Canonical (C14N), when
- You are computing or verifying a signature, where the byte stream is the thing being signed.
- You want to know whether two documents are equivalent, and a text diff is drowning you in noise.
- You are deduplicating documents by hash and re-serialization keeps producing new hashes.
Minified, when
- You are shipping bytes over a network and nothing downstream compares them.
- The document has no mixed content, so the whitespace between elements is genuinely disposable.
Where it reaches
- ToolsXML canonicalizer (C14N)All three C14N modes, on your own document.
- ToolsXML minifierThe other transform, which only touches whitespace.
- NamespacesXML SignatureThe specification that made canonicalization necessary.
- ToolsXML diffThe third way to compare two documents: structurally.
Related comparisons
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.