Minify a Simple XML Message Before Comparing Payload Size

Compare formatted and compact output for a fictional XML message while preserving a source copy and watching whitespace-sensitive content.

By DevSphere Editorial · · 5 min read

Open XML Tools

Use minification only for a measured task

Removing indentation and comments can make a short XML document easier to embed in a test fixture or compare by character count, but it is not compression and may remove meaningful content. XML Tools has a Minify subtool that applies regular-expression replacements to whitespace and comments. It does not parse the document before transforming it and is not a canonicalizer. This workflow uses a simple element-only demo message without mixed content, CDATA, signatures, or formatting-sensitive text. Keep a safe copy of the original and treat the result as a derived sample. The tool can report the input length in characters and lines, but it does not calculate transfer size after compression or evaluate network performance. Use the actual downstream parser to confirm the compact output is accepted.

<?xml version="1.0"?>
<notice>
  <kind>demo</kind>
  <count>3</count>
</notice>

Create a readable baseline

Open XML Tools and select Formatter. Paste the sample into XML Input and use two-space indentation. Check that the input contains an XML declaration, then inspect the formatted output for the root notice and its kind and count children. Formatter first asks the browser DOMParser to parse the document, then tokenizes tags and text to add line breaks. The output is useful as a baseline for a human comparison. It is not a fully spec-aware pretty printer; unusual mixed text, whitespace nodes, or processing instructions should be tested separately. Copy Output only if you need to preserve the rendered baseline elsewhere. Do not replace the source file with formatted output until you have checked whether comments, line endings, or exact whitespace matter to the receiving application.

Switch to Minify and compare

Choose Minify in the XML subtool navigation. The output removes comments, collapses whitespace between tags, compresses repeated whitespace, and trims spaces around markup. The resulting sample should be a compact declaration followed by a notice element with kind and count children. The input statistics show characters and lines, allowing a rough before-and-after character comparison if you record both values. These numbers do not account for gzip, transport framing, or caching and should not be used to claim a performance improvement. Minify has no indentation control because its goal is a single compact text representation. Copy Output can copy the result. Keep original and derived text clearly named in your own process to avoid accidentally submitting a demonstration payload in place of production input.

<?xml version="1.0"?><notice><kind>demo</kind><count>3</count></notice>

Protect text whose spaces carry meaning

Whitespace can be data in XML, particularly in mixed content or inside elements where exact text is significant. The Minify implementation collapses runs of whitespace globally and removes comments, so it can change text nodes and delete comments that a documentation workflow expects to retain. CDATA, entity references, processing instructions, and namespace details should also be checked with a real parser and the target application. Do not minify signed XML, canonicalized documents, or content whose signature includes whitespace without using the required XML canonicalization procedure. A syntactically valid result is not necessarily semantically identical. This simple notice avoids those risky cases, but the limit still applies. Use a proper XML-aware serializer or transformation pipeline for more complex documents.

Verify the output and recover from surprises

If the compact output is empty, confirm that XML Input is not blank and that the Minify subtool is selected. Unlike Formatter, this minification step does not parse first, so malformed XML may be compacted without being corrected. Return to Validator to check well-formedness after minification and compare the root and child text with the original. If the document contains comments, verify their intentional removal; if it contains meaningful spaces, do not use this output. XML Tools offers a DOM-based Validator for basic well-formedness and some warnings, but it does not validate XSD or business rules. Keep the unmodified source and run the actual consumer's tests. Clear the editor between payloads, use invented content for examples, and treat character count as a descriptive measurement rather than an optimization guarantee.

Do not interpret the Minify output as a canonical XML form. Canonicalization has defined rules for namespaces, attributes, and other details, while this component uses regular-expression substitutions over text. A comment is removed unconditionally, and repeated whitespace is collapsed even when it could be part of a text node. For a payload whose meaning depends on whitespace, compare parsed node values before and after with an XML-aware test instead of comparing only rendered strings. If you are merely preparing a compact snippet for a slide or a small fixture, note that it is illustrative and retain the pretty original nearby. If you are measuring actual message transport, measure the encoded payload in its real pipeline and include the normal compression and envelope behavior. A shorter string in this editor is not evidence of faster delivery.

A safe check for this simple notice is to run Validator before and after and compare the parsed root name and the kind and count text. That still does not prove equivalence for all XML semantics, but it catches an obvious truncation or tag damage in this particular case. Use the XML Viewer to inspect the resulting tree if an output line seems difficult to read. Do not rely on comments to carry instructions that must survive: minification removes them by design. If a downstream system signs XML or computes a digest, follow its prescribed canonicalization and signing sequence instead of sending this minifier's output. Keep an unmodified fixture in the test suite so future changes to the helper's regex behavior can be evaluated against a known sample.

Browse all tutorials