How to use the xml minify
- Paste or type your XML into the input area.
- Check Keep processing instructions if you need to preserve
<?xml ...?>declarations. - The tool automatically validates and compresses the XML as you type.
- If an error is found, the message shows the exact line and column.
- Copy the compact result from the output box.
How the xml minify process works
The tool uses a hand-written tokenizer that reads the input character by character, tracking line and column numbers for error reporting. It recognizes elements, attributes, text nodes, CDATA sections, comments, processing instructions and entity references.
During the xml minify pass, comments are discarded entirely. Processing instructions are kept only when the option is enabled. Whitespace-only text between tags is dropped. In other text nodes outside CDATA, runs of whitespace collapse to a single space and the edges are trimmed, except that one space is kept next to a neighbouring element so mixed content such as mixed <b>bold</b> tail keeps its word breaks. Attribute values inside tags are preserved exactly as written, including their internal spacing.
A stack tracks open element names. When a closing tag is encountered, its name must match the top of the stack. If the stack is not empty at the end of the input, or if a mismatch occurs, the tokenizer stops and reports the position of the error.
What is preserved and what is removed
Understanding what changes during minification helps avoid surprises. Here is a summary of how each XML construct is handled:
| Construct | Action |
|---|---|
Comments <!-- ... --> | Removed completely |
Processing instructions <?...?> | Removed by default, kept if option is checked |
CDATA sections <![CDATA[...]]> | Preserved exactly, including all internal whitespace |
| Text between tags | Whitespace-only nodes dropped; other text collapsed to single spaces and trimmed (one edge space kept beside an element) |
| Attribute values | Preserved exactly as written |
| Entity references | Validated but left encoded (e.g., &) |
| Tag structure | Preserved; self-closing tags stay self-closing |
Supported entities and references
The tokenizer validates five predefined XML entities: &, <, >, " and '. Numeric character references in decimal (A) and hexadecimal (A) form are also accepted. Any other named entity reference triggers a validation error because resolving it would require an external DTD.
This tool does not process DOCTYPE declarations or resolve external entities. If your XML relies on a DTD for entity definitions, you should resolve those entities before you xml minify the document. For inspecting raw XML structure before or after compression, you can use an xml viewer.
Practical use cases
Minifying XML reduces file size by removing bytes that carry no semantic meaning. This matters most in two scenarios: network transfer and storage. When an API sends large XML payloads, stripping comments and whitespace can reduce the byte count by ten to thirty percent depending on how heavily the original was formatted.
Configuration files that are read by machines rather than humans benefit from minification too. A build system that packages XML resources into an application can run them through a minifier to shrink the final bundle. If you later need to convert that compact XML into another format, tools exist to transform it to CSV, YAML or HTML.
Common mistakes and edge cases
Whitespace handling is the most frequent source of confusion. In mixed content, where text and child elements alternate, collapsing whitespace can change rendering in formats like XHTML. The minifier collapses runs of whitespace inside text to one space, which is correct for data-oriented XML but can still alter documents where whitespace is significant (for example elements with xml:space="preserve", which this tool does not honour).
Another edge case involves attribute ordering. XML treats attribute order as insignificant, and this tool preserves whatever order appears in the source. Self-closing tags like <br/> remain self-closing rather than being expanded to <br></br>.
If you need to compare a minified file against a formatted original, structural comparison is more reliable than string comparison. An xml diff tool handles this by parsing both documents first.
Worked examples
| Example | Input | Result |
|---|---|---|
| Simple document | XML input: <catalog> / <book id="1"> / <title>XML Basics</title> / </book> / </catalog>; Keep processing instructions: no | <catalog><book id="1"><title>XML Basics</title></book></catalog> |
| With CDATA and comment | XML input: <data> / <!-- secret --> / <![CDATA[<not>xml</not>]]> / </data>; Keep processing instructions: no | <data><![CDATA[<not>xml</not>]]></data> |
| Processing instruction | XML input: <?xml version="1.0"?> / <root/>; Keep processing instructions: yes | <?xml version="1.0"?><root/> |
FAQ
Does this xml minify tool validate my XML?
Yes, for basic well-formedness. The tokenizer checks that every opening tag has a matching closing tag, that there is a single root element, that attributes are quoted and not duplicated, and that only valid entity and character references are used. It is not a full XML validator and does not read DOCTYPE declarations. If it finds a problem, it reports the exact line and column.
Are CDATA sections modified during minification?
No. Everything inside a CDATA section is preserved exactly as written, including all whitespace and special characters. Only regular text nodes outside CDATA are trimmed and collapsed.
Can I keep the XML declaration at the top?
Check the "Keep processing instructions" option. This preserves the declaration and any other processing instructions. By default they are removed to achieve maximum compression.
Is my XML uploaded to a server?
No. The entire minification process runs locally in your browser using JavaScript. Nothing is sent over the network.
Why does the tool reject some entity references?
Only the five predefined XML entities and numeric character references are supported. Custom entities defined in a DTD are rejected because the tool does not load or parse external schemas.
Will minifying break my XML schema validation?
Minification removes comments and formatting whitespace. Structure, element order and attributes are unchanged, but whitespace inside text is collapsed, so a schema that constrains exact whitespace in text values (or xml:space="preserve" content) could see a difference.