IZN Tools

XML Formatter

Indent XML and fold long attribute lists down to size.

INPUT
—
OUTPUT
Waiting for input
Computed on this device

Formatting is for reading

XML arrives from an API or a config export as one line, and the shape of the document is invisible. Indenting makes the nesting legible: which elements are siblings, which are children, where the attributes sit.

The one thing to know before reformatting is mixed content. In a vocabulary where text and elements share a parent — a paragraph containing <em> — the whitespace you add is not decoration, it is text. Formatting such a document changes it. For data-shaped XML, which is nearly all XML in practice, indenting is safe.

What survives

  • <![CDATA[...]]> — untouched, including whitespace and < characters
  • <!-- comments --> — content preserved
  • <?xml ... ?> and doctype declarations — kept at the top, unindented

Collapsing short text

An element whose only child is short text is kept on one line by default, because three lines to say Acme reads worse than one. Turn it off if you need every element on its own line for diffing.

Questions

Does formatting change the document?+

Whitespace between elements is insignificant in most XML vocabularies, so indenting is safe there. It is not universally safe: in a document with mixed content — text and elements in the same parent, as in XHTML — added whitespace is part of the text. Formatting is for reading; do not commit reformatted XML to something that compares it byte for byte.

Are CDATA and comments preserved?+

Yes, both, byte for byte. CDATA exists precisely to hold text that must not be interpreted, and reindenting inside it would defeat the point. Comments keep their content, and conditional sections are left alone.

What about a signed XML document?+

Do not format it. An XML signature covers a canonical form of the document, and reindenting invalidates it. The same applies to anything with a hash or a digest attached.

Why does my minified output still have spaces?+

Inside text content. Minifying removes formatting whitespace between tags, not the text a person wrote — collapsing that would change the data.