{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). A colon written literally in the XML (a namespace prefix such as dc:creator, a time such as 12:30) is a parameter separator and cuts the XML off - write it as #: . Colons in XML that comes from a field or another command need no escaping. Without an encoding declaration in the input, accented and other non-ASCII letters come back as numeric character references (for example č); start the XML with an XML declaration that has encoding="UTF-8" to keep them as they are - the output then begins with that declaration. An empty or blank value gives an empty string. Until November 2026 it stopped the whole page with a fatal error. On an install updated since late October 2026 but not since November, guard a field that may be empty with ifset, which expands only the branch it chooses; on an older install the call inside ifset still runs, so there make sure the value is never empty. The output is raw XML, which a browser would treat as tags; to show it on a page wrap it in safe (inside a pre element to keep the indentation). Raw calls are therefore shown here as illustrative, while the asserted examples use safe or strlen.

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. An empty or blank value gives an empty string (until November 2026 it stopped the page with a fatal error).

Examples

test[{xmlformat:}]
Expected[]
Actual[]
An empty or blank value gives an empty string. Until November 2026 it stopped the whole page with a fatal error.
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{safe:{xmlformat:<a><b><c>x</c></b></a>}}
Expected<?xml version="1.0"?> <a> <b> <c>x</c> </b> </a>
Actual<?xml version="1.0"?> <a> <b> <c>x</c> </b> </a>
Wrapped in safe so the page shows the formatted XML as text. The result starts with the XML declaration, puts every element on its own line, indents each level by two spaces, and ends with a newline.
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.
test{strlen:{xmlformat:<a b="12#:30"/>}}
Expected37
Actual37
A colon typed into the template splits the parameter, so 12:30 would leave only the broken start of the XML (and the result would be the 22-byte declaration alone). Written as 12#:30 the whole attribute survives and the formatted document is 37 bytes.
test[{ifset::{xmlformat:}}]
Expected[]
Actual[]
Until November 2026 a bare xmlformat with an empty value stopped the page with a fatal error; now it gives an empty string. On an install updated since late October 2026 but not since November, guard the call with ifset: it expands its second parameter only when the first one is not empty, so put the field first and the xmlformat call second. Older installs expand the call inside ifset anyway, so there the value must never be empty. Here the first parameter is empty, so nothing is formatted.