XML Minifier
Losslessly strip ignorable whitespace and comments from XML — attribute, text, and CDATA content is preserved byte-for-byte.
How XML minification works
XML documents are often hand-formatted with indentation and line breaks for readability, but that whitespace between elements isn’t required by the format — it just adds bytes. This minifier parses your document with the browser’s native XML engine and re-serializes it, removing comments (<!-- ... -->) and the ignorable whitespace between tags.
Crucially, it is lossless: because it works on the parse tree rather than a text search-and-replace, attribute values, significant text content, and <![CDATA[ ... ]]>sections are preserved byte-for-byte — a naive regex minifier silently collapses the spaces inside all of those. Invalid XML is reported with its line and column instead of being mangled. Everything runs locally in your browser.
Built and maintained by Meet Shah · Last updated
What this tool is used for
- Shrinking an XML payload before sending it over a constrained channel.
- Removing comments from a document before publishing it.
- Producing a single-line document for a field that rejects line breaks.
- Measuring how much of a document is formatting rather than data.
- Comparing two documents' sizes with formatting removed from both.
Frequently Asked Questions
- What can safely be removed from XML?
- Whitespace between elements, comments, and the indentation of the document — none of which carries meaning to a parser. What cannot be touched is whitespace inside element content, which is data unless a schema explicitly says otherwise.
- Why is `xml:space="preserve"` important?
- Because it marks an element whose whitespace is significant, and a minifier that ignores it destroys data. Mixed content — text with inline markup inside it — is the other case where collapsing spaces changes what the document says.
- How much smaller does minifying make a file?
- Typically 20–40% for a pretty-printed document, which sounds worthwhile and usually is not. Gzip already compresses repeated indentation almost to nothing, so the transferred size barely moves — the saving is real only for uncompressed storage.
- Does minifying break a signature?
- Almost always. XML Signature covers a canonicalised form, and minification is a different transformation, so any change to whitespace invalidates the digest. Signed documents must not be minified after signing — this is the one case where it is destructive.
- Should the XML declaration be kept?
- Yes. It is small and it states the encoding, which a parser needs when there is no external declaration to rely on. Removing it to save thirty bytes risks the whole document being misinterpreted, which is a poor trade.
Common errors and gotchas
- Collapsing whitespace inside an element where it was significant, which changes the data.
- Stripping a CDATA section's contents, which must be preserved exactly.
- Removing comments that were processing directives rather than notes.
- Assuming the saving matters after compression, since whitespace compresses very well.
- Minifying a document whose schema declares whitespace-preserving elements.