An XML formatter parses your document and rewrites the whitespace between elements, so each nested element starts on its own indented line. An XML validator first checks that the document is well-formed: one root element, every tag closed and correctly nested, attribute values in quotes, and & and < escaped in text and attribute values.
Full validation against a schema (XSD, DTD or RELAX NG) is a separate, stricter step. The XML Formatter & Validator formats, minifies and checks XML in your browser, and nothing you paste is uploaded.
How to format XML online with Zutilium's XML formatter
- Open the XML Formatter & Validator and paste your document into the XML input box. It takes up to 524,288 characters, with one root element and no
<!DOCTYPEdeclaration. - Under Indentation, choose 2 spaces or 4 spaces. Set it before you format, because changing it clears the result.
- Select Format for an indented copy, Minify for a compact one, or Validate to check the syntax without producing output.
- Read the status line below the boxes. "Valid XML. Result ready." or "Valid XML syntax." means the document is well-formed. Otherwise the line starts with "Invalid XML." followed by your browser's parser message, which usually names the line and column of the first problem.
- Select Copy result, or Download result to save a UTF-8 file named document.xml.
Only the whitespace between elements should change. Your browser's serializer also rewrites a few things every XML parser reads the same way: attribute values get double quotes, an empty pair such as <tag></tag> becomes <tag/>, numeric character references such as © become the characters they stand for, and an encoding value in the XML declaration becomes UTF-8 to match the download. Comments, processing instructions and CDATA sections stay, even with Minify, so delete comments yourself if you need the smallest payload.
Worked example: pretty print a one-line RSS feed
Feeds, sitemaps and API responses often arrive minified. This RSS feed is one line of 442 characters:
<?xml version="1.0" encoding="ISO-8859-1"?><rss version="2.0"><channel><title>Shop news</title><link>https://example.com/</link><!-- built nightly --><item><title>Winter sale</title><link>https://example.com/sale?utm_source=rss&utm_medium=feed</link><description><![CDATA[Up to <b>30%</b> off & free returns]]></description><enclosure url="https://example.com/sale.mp3" length="48213" type="audio/mpeg"></enclosure></item></channel></rss>
Format with 2 spaces returns 487 characters:
<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
<channel>
<title>Shop news</title>
<link>https://example.com/</link>
<!-- built nightly -->
<item>
<title>Winter sale</title>
<link>https://example.com/sale?utm_source=rss&utm_medium=feed</link>
<description><![CDATA[Up to <b>30%</b> off & free returns]]></description>
<enclosure url="https://example.com/sale.mp3" length="48213" type="audio/mpeg"/>
</item>
</channel>
</rss>
- Each level is indented by two spaces. 4 spaces makes the same feed 535 characters.
- The declaration now says
encoding="UTF-8", and the emptyenclosurepair became one self-closing tag. - The comment kept its place, and the CDATA description, raw
<b>and&included, is unchanged. - Minify turns either version into one line of 426 characters: 16 fewer than the original, 5 from the shorter encoding name and 11 from the self-closing tag.
xmllint --format gives the same layout but keeps the ISO-8859-1 declaration and writes the file in that encoding.
Well-formed vs valid: what an XML validator checks
The XML 1.0 specification defines two levels. A well-formed document follows the syntax rules all XML shares, so any XML parser can read it.
A valid document also follows the rules for its type of document: which elements may appear, in what order, with which attributes and values. The specification ties validity to a DTD; in everyday use the word also covers XSD and RELAX NG schemas.
| Check | Well-formed | Schema-valid |
|---|---|---|
| One root, tags closed and nested | Yes | Yes |
Quoted attributes, escaped & and < |
Yes | Yes |
| Allowed element names and order | No | Yes |
| Required attributes | No | Yes |
| Data types such as decimals | No | XSD and RELAX NG |
| Needs a schema file | No | Yes |
Zutilium's Validate checks well-formedness only, so this order passes:
<order id="1042">
<customer>Ana López</customer>
<total>12,50</total>
</order>
Its schema requires an id attribute, then a customer, then a total of type xs:decimal, which is written with a decimal point:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="order">
<xs:complexType>
<xs:sequence>
<xs:element name="customer" type="xs:string"/>
<xs:element name="total" type="xs:decimal"/>
</xs:sequence>
<xs:attribute name="id" type="xs:positiveInteger" use="required"/>
</xs:complexType>
</xs:element>
</xs:schema>
$ xmllint --noout --schema order.xsd order.xml
order.xml:3: element total: Schemas validity error : Element 'total': '12,50' is not a valid value of the atomic type 'xs:decimal'.
order.xml fails to validate
Swap the two child elements and xmllint reports This element is not expected. Expected is ( customer ). instead. Remove the id attribute and it reports The attribute 'id' is required but missing.
A DTD with the same structure catches the swapped elements but accepts 12,50, because DTDs cannot give text a data type. A document that is not well-formed never reaches any of these checks, so fix the syntax first.
Common XML errors and how to fix them
The messages below come from xmllint (libxml2 2.9.13, as included with macOS 26). Zutilium shows your browser's parser message instead; the wording varies by browser, but the problem is the same.
| Mistake | Example | xmllint message | Fix |
|---|---|---|---|
| Unclosed tag | <order><id>42</order> |
Opening and ending tag mismatch: id line 1 and order |
<id>42</id> |
| Case mismatch | <Name>Ana</name> |
Opening and ending tag mismatch: Name line 1 and name |
Match the case |
| Bare ampersand | Smith & Sons |
xmlParseEntityRef: no name |
Smith & Sons |
| Ampersand in a URL | ?a=1&b=2 |
EntityRef: expecting ';' |
?a=1&b=2 |
| Less-than in text | a < b |
StartTag: invalid element name |
a < b |
| Less-than in an attribute | title="5 < 6" |
Unescaped '<' not allowed in attributes values |
title="5 < 6" |
| Unquoted attribute | <item id=7/> |
AttValue: " or ' expected |
<item id="7"/> |
| Two root elements | <a/><b/> |
Extra content at the end of the document |
Wrap both in one root |
| HTML entity | |
Entity 'nbsp' not defined |
  |
| HTML-style empty tag | <p>One<br>Two</p> |
Opening and ending tag mismatch: br line 1 and p |
<br/> |
| Text before the declaration | A blank first line | XML declaration allowed only at the start of the document |
Delete it |
| Cut-off document | <a><b>text</b> |
Premature end of data in tag a line 1 |
Restore the end |
| Not XML | {"id": 1} |
Start tag expected, '<' not found |
Check what you pasted |
Treat the position as the place where the parser noticed the problem, not where it began. For an unclosed tag, the message names the element left open and the line where it started (id line 1), while the position points at the end tag that did not match. xmllint often prints several errors for one mistake (four for the unquoted attribute), so fix the first and run it again.
Which ampersand message you get depends on what follows the &: a space gives no name, a word without a closing semicolon gives expecting ';'. URLs with query strings are the usual source; the sitemap protocol, for example, requires every & in a sitemap URL to be written as &.
An undeclared namespace prefix, such as media:content with no xmlns:media declaration, breaks the Namespaces in XML rules. xmllint prints Namespace prefix media on content is not defined but still exits with status 0, so scripts that only check the exit code miss it.
Pretty print XML without changing mixed text or CDATA
Pretty printing is safe for data-style XML, where each element holds either child elements or text. Document-style XML, such as XHTML, DocBook or a product description, mixes text with inline elements, and there whitespace is content: a line break added between two inline elements changes the text.
Zutilium's formatter reindents an element only when it has child elements and nothing between them except comments, processing instructions or whitespace that includes a line break. It leaves an element, and everything inside it, exactly as written when the element:
- contains text, as in
<p>Press <kbd>Ctrl</kbd>+<kbd>S</kbd> to save.</p> - contains a CDATA section
- has
xml:space="preserve", the standard way to ask for whitespace to be kept (XML 1.0, section 2.10) - separates its children with spaces on the same line, as in
<b>Hello</b> <i>world</i>
Other formatters treat the same markup differently (libxml2 2.9.13 and 2.10.3, Python 3.11):
| Formatter | Text mixed with tags | Space between inline tags | xml:space="preserve" |
|---|---|---|---|
| Zutilium Format | Kept | Kept | Respected |
xmllint --format |
Kept | Becomes a line break | Respected |
Python ElementTree.indent() |
Kept | Becomes a line break | Ignored |
Python minidom toprettyxml() |
Split onto new lines | Kept, plus new lines | Ignored |
One case defeats every formatter: inline elements with no text between them. <p><b>Hello</b><i>world</i></p> looks like data, so all four put the children on separate lines, adding whitespace between the words. Mark such blocks xml:space="preserve", or leave document-style files unformatted.
CDATA sections pass through untouched. Inside one, only the ]]> end marker counts as markup, so < and & need no escaping, handy for code and HTML snippets. To include ]]> itself, split it across two sections: <![CDATA[x ]]]]><![CDATA[> y]]> holds the text x ]]> y.
Format and validate XML on the command line with xmllint
xmllint is part of libxml2. macOS includes it, and on Debian and Ubuntu it comes in the libxml2-utils package. feed.xml and order.xml are the examples above, and order.rng and order.dtd describe the same structure as order.xsd:
# Well-formed? Prints nothing and exits with 0 if the file parses
xmllint --noout feed.xml
# Pretty print with two spaces, or set your own indent string
xmllint --format feed.xml > feed-pretty.xml
XMLLINT_INDENT=' ' xmllint --format feed.xml
# Validate against an XSD, a RELAX NG schema or a DTD
xmllint --noout --schema order.xsd order.xml
xmllint --noout --relaxng order.rng order.xml
xmllint --noout --dtdvalid order.dtd order.xml
# Copy without the DOCTYPE, with the entities it defined expanded
xmllint --loaddtd --noent --dropdtd with-doctype.xml > clean.xml
XSD and RELAX NG failures end with fails to validate, and all three schema checks exit with status 3 on a mismatch. --valid checks a document against the DTD named in its own DOCTYPE. The xmllint manual itself warns that --format has never worked reliably, especially without a DTD; the space-between-tags change in the table above is one example.
Troubleshooting
"Documents containing a DOCTYPE are unsupported"
Delete the <!DOCTYPE declaration and run it again. If the document then fails with Entity 'nbsp' not defined or a similar message, the DTD defined those entities: replace each with a numeric reference such as  , or let the last xmllint command above expand them and drop the DOCTYPE in one step. Run it only on files you trust: it loads external DTDs and entities.
Format leaves part of the document on one line
That element contains text, a CDATA section or xml:space="preserve", or its tags are separated by spaces with no line break, so the formatter treats its whitespace as content. Minify follows the same rule. If the spaces are only padding, as in <root> <a>1</a> <b>2</b> </root>, delete them or run xmllint --format, then select Format again.
The error is at line 1, column 1
The text does not start with markup, so it is probably not XML at all. Minified XML is one long line, so every error in it is on line 1; check the column instead.
A long run of letters, digits, + and / with no angle brackets looks like Base64-encoded data: decode it with the Base64 Encoder & Decoder first. Text that starts with { or [ is probably JSON, which the JSON Formatter & Validator checks; the guide to fixing invalid JSON covers its errors.
Size and depth limits
Input is capped at 524,288 characters. Format stops with "Formatted text exceeds 4 million added characters." when the indentation it adds grows that large, so switch to 2 spaces or use Minify.
Elements nested more than 128 levels below the root stop both Format and Minify with "Maximum formatting depth of 128 exceeded.", while Validate still works. For bigger or deeper files, use xmllint.
Frequently asked questions
What is the difference between well-formed and valid XML?
Well-formed XML follows the syntax rules all XML shares: one root element, closed and properly nested tags, quoted attribute values and escaped & and <. Valid XML is well-formed and also matches a schema, such as a DTD, XSD or RELAX NG file, that defines the allowed elements, attributes and values. A document can be well-formed without being valid, but never valid without being well-formed.
Does formatting XML change the data?
It changes the whitespace between elements, which most programs that read XML ignore. Whitespace does matter in mixed content, where text and inline elements share a parent, so a careful formatter leaves those elements alone. Quote style, self-closing tags and character references may also change, but every XML parser reads them as the same data.
How do I validate XML against an XSD?
Use a schema-aware validator, such as xmllint --noout --schema schema.xsd file.xml on macOS or Linux, or the XML Language Support extension by Red Hat, which adds XSD and DTD validation to VS Code. Zutilium's validator checks well-formedness only, which a document has to pass before any schema check can run.
Can VS Code format XML?
Not out of the box. The VS Code documentation lists built-in formatters for JavaScript, TypeScript, JSON, HTML and CSS, so XML needs an extension such as XML Language Support by Red Hat. Then run Format Document: Shift+Alt+F on Windows, ⇧⌥F on macOS or Ctrl+Shift+I on Linux.
Is it safe to format XML online?
Check where the processing happens before you paste anything sensitive, such as an API response with tokens or customer data. Zutilium's formatter does all of its work on your device with the browser's own XML parser, and because it rejects DOCTYPEs, it never reaches out for external DTDs or entities.