{xmlformat:xml}

Description

Pretty-prints an XML string: parses it with DOMDocument and re-serializes it with formatOutput on, so the result is reindented with two-space steps, one element per line, and an XML declaration line prepended (version 1.0). The single parameter is the XML to format; it must be well-formed and have a single root element. On malformed or non-XML input the parser fails quietly and the command returns just that one declaration line followed by a newline (22 bytes). Empty input is a hard error - DOMDocument loadXML throws on an empty string - so never pass an empty value. Because the output carries real angle-bracket tags, the live result of a raw call is shown on this page as Illustrative rather than asserted; the asserted examples wrap the call in strlen to get a deterministic, tag-free byte length.

Parameters

xml required default (none)

The XML to pretty-print. Must be well-formed and have a single root element. On malformed or non-XML input the command returns just the XML declaration line. Do not pass an empty value - DOMDocument throws on empty input.

Examples

virtual{xmlformat:<note><to>Tove</to><from>Jani</from><body>Hello</body></note>}
Expected(the same document reindented: a declaration line, then each element - note, to, from, body - on its own line with two-space indentation)
Actual Tove Jani Hello
The typical use: feed compact XML, get it back reindented with two-space steps, one element per line, and an XML declaration on top. The live result carries real tags, so it is shown here as Illustrative rather than asserted.
test{strlen:{xmlformat:<root><a>1</a><b>2</b></root>}}
Expected59
Actual59
Because the raw output carries tags, the asserted examples wrap xmlformat in strlen to get a deterministic byte count. This proves the exact formatted string was produced: the 21-char declaration line plus a newline, then the reindented root with two children, 59 bytes total.
virtual{xmlformat:<a><b><c>x</c></b></a>}
Expected(a declaration line, then element a at column 0, b indented two spaces, c indented four spaces, each on its own line)
Actual x
Each nesting level adds two more spaces of indentation, so deep structures become a readable tree. Illustrative (the output carries tags).
test{strlen:{xmlformat:not xml}}
Expected22
Actual22
The parser does not throw on malformed-but-nonempty input; it fails quietly and saveXML returns just the declaration line. strlen of that line is 22 (21 chars plus the trailing newline). Use this to detect parse failures.