An XML formatter takes XML input and re-emits it with consistent indentation and line breaks. The tool above runs the formatter in your browser; nothing leaves your machine. For minification (reverse direction), most XML tools don’t bother — XML is verbose by design.
What gets indented
Each element is indented relative to its parent:
Input:
<root><parent><child>value</child></parent></root>
Output:
<root>
<parent>
<child>value</child>
</parent>
</root>
Default indent is 2 spaces; common alternatives are 4 spaces or tab.
Attribute formatting
Elements with many attributes can be broken across lines:
<input
type="email"
name="user-email"
required="required"
placeholder="you@example.com"
/>
The threshold (how many attributes before breaking) varies between formatters. The tool above doesn’t wrap attributes: every start tag stays on one line, however long.
What’s whitespace-sensitive
XML has elements where whitespace matters:
xml:space="preserve"attribute tells the parser to preserve all whitespace inside the element. Formatters should respect this.- CDATA sections preserve whitespace verbatim:
<![CDATA[
This whitespace
is preserved
exactly.
]]>
The formatter must not reflow CDATA content.
- Mixed content (text and elements interleaved) is sensitive:
<p>Some <em>italic</em> text in a <strong>paragraph</strong>.</p>
The whitespace between text and inline elements is significant. A formatter that adds line breaks here changes rendering. The tool above keeps mixed content on one line.
Comments and processing instructions
<!-- A comment -->
<?xml-stylesheet type="text/xsl" href="style.xsl"?>
Both are preserved as-is by the formatter, on their own lines, with appropriate indentation.
XML declaration
<?xml version="1.0" encoding="UTF-8"?>
Always at the top, no indentation. Required by some XML processors. The formatter preserves whatever declaration the source had.
Self-closing tags
<br/>
<input type="text" name="x"/>
<elem></elem> ← equivalent to <elem/> for empty content
The XML spec treats <elem></elem> and <elem/> as equivalent for empty elements. Most formatters convert empty <elem></elem> to self-closing <elem/> for compactness; the tool above always does.
Namespaces
<root xmlns:custom="http://example.com/ns">
<custom:item>value</custom:item>
</root>
Namespace prefixes are part of element names. Formatters preserve them. The default namespace (xmlns="...") and prefix declarations (xmlns:foo="...") are attributes, formatted like other attributes.
Doctype
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
The tool above copies the DOCTYPE declaration exactly as written, including any internal subset.
Validation vs formatting
A formatter parses XML enough to format it but doesn’t validate against a schema. To check that an XML document conforms to a specific XSD or RelaxNG schema, use a validator.
The formatter rejects malformed XML — unclosed tags, invalid characters, mismatched quotes — with a parse error. It accepts XML that’s well-formed but schema-noncompliant.
Idempotence
Format a file twice; the second pass should produce identical output. If a formatter is non-idempotent, it has a bug. Quick check: pipe a file through twice and diff the results.
What the formatter doesn’t do
- Doesn’t reorder attributes. The order is preserved as written. Some XML schemas require specific attribute order; respect what the source has.
- Doesn’t normalize whitespace inside text.
<p>hello world</p>keeps the multiple spaces (whitespace inside text is significant unlessxml:space="default"and the parser is told to normalize). - Doesn’t remove or merge namespaces. Each declaration stays as written.
- Doesn’t validate against a schema. Formatter ≠ validator. (The tool’s Validate mode checks well-formedness only.)
- Doesn’t convert encoding. Whatever encoding the input is in, the output stays the same (the declaration is preserved).
When the formatter is wrong for the job
- HTML masquerading as XML. HTML allows things XML doesn’t (unclosed
<br>, unquoted attributes, lowercased tags). Use an HTML formatter for HTML — see HTML formatter. - SVG. SVG is valid XML; the XML formatter handles it. But SVG has rendering-sensitive whitespace inside
<text>elements; check after formatting. - Office Open XML (Word, Excel, PowerPoint files). These are zipped XML. To format the inside, unzip, format individual XML files, re-zip. Don’t expect the formatter to handle the zip layer.
- Very large XML. A 1 GB XML file in a browser will run out of memory. Use streaming XML tools on the command line.
Common pitfalls
- Reformatting CDATA. Whitespace inside CDATA is significant. A formatter that re-indents CDATA content changes meaning.
- Reformatting whitespace-significant elements. Some XML dialects use elements where text whitespace is meaningful (HTML’s
<pre>, Word’s<w:t>). Check the spec for the dialect. - Lost attribute order. Most formatters preserve order; some don’t. If your XSD requires specific order, verify after formatting.
- Encoding mismatch. A UTF-16 file declared as UTF-8 (or vice versa) confuses the parser. Decode in the right encoding before formatting.
- BOM bytes. A UTF-8 BOM at the start is preserved by some formatters and stripped by others. The XML spec says it’s allowed but not required; some XML parsers reject documents with BOM.
For other format tools, see JSON formatter, HTML formatter, CSS formatter, SQL formatter.