Skip to main content
XMLDir

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

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

Canonical (C14N) compared with Minified, one row per aspect
AspectCanonical (C14N)Minified
PurposeMake equivalent documents byte-identicalMake the document smaller
XML declarationRemovedKept
Attribute orderSortedLeft as written
Empty elementsExpanded to a start and end tagLeft as written
Whitespace between elementsKept — it is contentRemoved where it is safe to
CDATAReplaced with escaped textKept
Use it forSignatures, comparison, deduplicationTransfer 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.

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.