What the XML minifier removes
- Indentation and line breaks between elements. In element-only content, these spaces are not data.
- Comments, unless you turn the option off.
- The spaces around text in simple elements, when the Trim option is on.
- The XML declaration, if you choose to remove it.
Example
<order id="42">
<!-- customer data -->
<customer>Alex</customer>
<items>
<item sku="A1" qty="2"/>
<item sku="B7" qty="1"/>
</items>
</order>
becomes:
<order id="42"><customer>Alex</customer><items><item sku="A1" qty="2"/><item sku="B7" qty="1"/></items></order>
What is never changed
- Mixed content, such as
<p>Ships in <em>2 days</em> worldwide.</p>. The spaces here belong to the text, so the minifier copies the element exactly. - Elements with
xml:space="preserve". - CDATA sections, processing instructions and the DOCTYPE.
- The names, attributes and values of every element.
Why minify XML?
- To make a request or a message smaller, for example a SOAP call or a message in a queue.
- To put an XML document in a single line of a log, a config value or a database field.
- To remove comments that you do not want to ship.
Keep your expectations realistic. When a server sends XML with gzip compression, as most do, the white space is already compressed very well, so minifying makes a smaller difference on the network than it makes on disk. The tool shows both numbers, the minified size and the gzip size, so that you can see the real saving.
Trim spaces: one thing to know
With Trim on, the spaces at the start and the end of the text in a simple element are removed, so <name> Alex </name> becomes <name>Alex</name>. This is what most people want. If your data uses those spaces, switch Trim off. Then the text is copied as it is.
Options
Remove or keep comments, trim text or not, sort attributes, use double quotes for attributes, write empty elements as <a/> or as <a></a>, and keep, add or remove the XML declaration.
To read minified XML again, use the XML beautifier or the XML formatter. The XML formatting guide explains when to minify. Other minifiers: HTML minifier, CSS minifier and JSON minifier.