How to Minify XML: Remove Whitespace Safely
Last updated: · 3 min read
Indented XML is easy to read, but the spaces and line breaks add bytes, and every byte counts when a document travels over a network or sits inside a larger payload. The XML Minifier removes whitespace and line breaks between tags so the whole document fits on one line. It never touches text content, meaning the characters written between two tags, so a value such as Ink bottle keeps its double space. Comments and CDATA sections are carried over unchanged. If the XML is not well-formed, the tool shows the line where the error begins. Processing happens in your browser.
Formula
1. Parse the text according to the XML 1.0 syntax; if it is invalid, stop and report the error line. 2. Delete text fragments that lie between tags and consist only of whitespace. 3. Keep every character inside an element's text content, spaces included, exactly as it is. 4. Pass comments, CDATA sections and processing instructions through unchanged. 5. Join all fragments without adding any extra spaces and output the result as a single line.
This is not a mathematical formula but a parsing and joining algorithm. Only whitespace between tags and line breaks are removed; element names, attributes and text values stay the same. The syntax rules are defined in the W3C Recommendation Extensible Markup Language (XML) 1.0 (Fifth Edition).
What the inputs mean
- XML
- The XML text to minify. It can be indented or already on several lines. The document must be well-formed; otherwise the tool shows the error line instead of output.
Worked example: Minifying a three-line product list
- Input: an <order> document whose two <item> elements are indented by two spaces on separate lines.
- The line breaks and the two spaces before each <item> sit between tags, so they are removed.
- The texts Pen and Ink bottle are inside the elements; the double space in Ink bottle is kept.
- The fragments are joined into one line: <order><item>Pen</item><item>Ink bottle</item></order>
Result: The document carries the same data, now free of the whitespace that only served indentation.
How to read the result
Minification only targets whitespace between tags. Text inside an element may also contain spaces, and those are preserved. If another system reads the document by position or relies on whitespace between elements, test that system with the minified output before sending it. This is general information.
Common mistakes
- Minifying a file and saving only the result, which makes later edits hard; keep the readable original as well.
- Expecting spaces inside text values to disappear; only whitespace between tags is removed.
- Trying to minify invalid XML; if the document is not well-formed, no output is produced and the error line must be fixed first.
- Trying to edit a single-line result by hand; finding one mistake in one long line is slow.
- Assuming attribute values lose their spaces; attribute values are kept exactly as written.
Frequently asked questions
Does minifying XML change the data?
No. Element names, attributes and text values stay the same; only whitespace between tags and line breaks is removed.
Are comments removed?
No. Comments and CDATA sections are carried over into the output.
How much smaller does the file get?
It depends on how deeply the document is indented. Heavily indented files shrink noticeably, while a document that is already compact barely changes.
Can I restore the indentation later?
Yes, with the XML Formatter. The original line breaks do not come back, though, so keep the readable source if you need it.
The result is not exact or binding and is for information only; it is not an official document and cannot be used in official procedures. Confirm it with the competent authority or a professional before acting; no liability is accepted for losses arising from its use.