How to Minify XML (and When You Shouldn't)
Minifying XML strips the whitespace that makes it readable. Done right it's lossless; done carelessly it corrupts data. Here's how to tell the difference.
What minification actually removes
Minifying XML means stripping the whitespace that was added for readability: the indentation at the start of each line and the line breaks between elements. The goal is a single, compact line that parses to the exact same node tree as the formatted original.
The key word is between. Whitespace between elements — the gaps a formatter inserts to nest things — is insignificant and safe to remove. Whitespace inside an element that holds text is a different story, and it's where naive minifiers go wrong.
The part that's risky
Consider an element whose text matters:
<message>Dear customer,
thank you for your order.</message>
A careless minifier that collapses all whitespace would turn the newline and indentation inside <message> into a single space — or remove it entirely — changing the text the application receives. The same hazard applies to <pre>-style content, embedded code, and anything marked xml:space="preserve".
A correct XML minifier parses the document first, then removes only the whitespace-only text nodes that sit between elements. Text nodes that contain non-whitespace characters are preserved byte-for-byte. This is the same principle as formatting in reverse: operate on the parsed tree, never on the raw string with a blunt regex.
How much do you actually save?
For typical pretty-printed XML, minification removes indentation and line breaks and trims roughly 15–30% of the byte count. That's less than the savings you'd see minifying JSON, because XML spends so many bytes on the tags themselves — and tags survive minification untouched.
More importantly, that raw saving rarely translates to the wire. Almost all HTTP traffic is sent with gzip or Brotli compression, and those algorithms are extremely good at collapsing the repetitive whitespace that minification removes. After compression, a formatted and a minified document are often within a couple of percent of each other. Minifying before gzip mostly saves work that gzip was going to do anyway.
When to minify — and when not to
Worth minifying:
- Data stored at rest uncompressed, where every byte is billed (some document stores, message queues).
- Embedding XML inside another payload where compression isn't applied to that field.
- High-volume APIs without transport compression, where the cumulative byte saving is real.
Not worth it:
- Anything served over gzip/Brotli HTTP — compression already does the work.
- Config files, logs, or documents humans read and edit — minifying destroys readability for a negligible gain.
- Files in version control — minified XML produces useless one-line diffs.
Because minification is lossless for element-only content, it's safely reversible: run a minified document back through a formatter and you get readable XML again. Keep the formatted version as the source of truth and minify only at the point of use.
Minify your XML now
Strip inter-element whitespace safely — text content and significant whitespace are preserved. Runs in your browser, and you can re-format it any time.
Open XML Minifier →Frequently Asked Questions
Does minifying XML change its meaning?
It should not, if done correctly. Removing whitespace between elements is safe because it's insignificant. Removing whitespace inside elements that contain text is not safe — that whitespace can be data. A correct minifier strips only inter-element whitespace and leaves text content alone.
How much smaller is minified XML?
For typical pretty-printed XML, minifying removes indentation and line breaks and usually shaves 15–30% off the byte count — less than for JSON, because XML's tags are verbose. Over gzip or Brotli the real transfer difference is much smaller, since compression already collapses whitespace.
Should I minify XML before sending it over a network?
Usually it's not worth it. If the transport uses gzip or Brotli — as almost all HTTP does — compression removes most of the redundancy minification targets. Minify when bytes are billed or stored uncompressed; keep XML formatted when humans will read or debug it.
Can I un-minify XML back to a readable form?
Yes. Minification of element-only content is lossless, so running a minified document through a formatter restores readable, indented XML. Keep the formatted version as your source of truth and minify only at the point of use.