XML Formatting: Whitespace Is Content Too

Open Tool XML Formatter and Minifier

Indent to read, compress to ship

XML expresses structure through nesting, so formatting adds indentation and line breaks between elements to make levels visible, while compression strips inter-element whitespace into one line for transport and diffing. It looks like layout only, yet carries risk: whitespace in XML can belong to text, so stripping or adding it around mixed content changes what readers see. The tool does both directions and lets you copy the output. It touches only whitespace between elements, never tags, attributes or structure.

Steps

  1. Paste the XML, keeping the declaration and namespaces.
  2. Choose indent or compress depending on purpose.
  3. Run it, then check that each indent matches a nesting level.
  4. After compressing, look for glued tags and intact attribute quotes.
  5. Copy the result and diff against the source to confirm only whitespace moved.
  6. Reset for the next document.

Mixed content, CDATA and declarations

Mixed content is the danger zone: text with tags inside, where the spaces around tags are real content, so inserting line breaks and indentation adds whitespace to what readers see. CDATA keeps characters verbatim and formatting must not touch it. Comments and processing instructions survive but may shift position with indentation. Namespace prefixes cannot change, since that alters meaning. Attribute order usually holds under formatting though some compressors reorder, so diffs need care. And a declared encoding must match reality, or Chinese turns to mojibake.

Common mistakes

Feeding HTML fragments to an XML formatter fails on unclosed tags. Formatting mixed content inserts spaces into body text. Compression strips whitespace inside CDATA and alters content. Deleting the encoding declaration turns Chinese into question marks. Unescaped quotes in attributes break parsing. And diffing a compressed one-liner shows nothing at all.

Where XML tools misfire

HTML is not strict XML, and error tolerance differs enough that page source usually fails, so use an HTML formatter. Formatting cannot repair structure, and unclosed tags need fixing at source. Signed or digested XML fails on any whitespace change. Very large files choke a browser, where chunking is steadier. And overwriting live files without a backup and a diff invites silent breakage.

⏫︎