Skip to content
CornerTools

XML Formatter

Format, validate and minify XML, with the exact line of any error.

1
1
Characters: 0
Format
Indent
Over
attributes

Paste or open an XML file to indent it for reading or minify it for sending, and see at once whether it is well-formed. An error names its line and column, and the line is marked in the input.

Formatting keeps the document as written: entity references stay unexpanded, attributes keep their order and quotes, and text whose whitespace matters is left alone. It runs in your browser, so nothing you paste or open is sent anywhere.

Options

  • Format — Pretty puts each element on its own line, indented by its depth. Minified removes the whitespace between tags.
  • Indent — 2 spaces (the default), 4 spaces or a tab, for Pretty.
  • Self-close empty elements — writes <a></a> as <a/>, in either format. The two mean the same in XML.
  • Attributes on separate lines — for Pretty, puts each attribute on its own line when an element has more than the number you set (3 by default).
  • Remove comments — for Minified.

Opening a file reads its encoding the way an XML parser does: a byte order mark first, then UTF-16 recognised from the first bytes, then the encoding named in the XML declaration. The output is UTF-8, so a declaration naming another encoding is rewritten to encoding="UTF-8", and a note says so. Without that rewrite, a program reading the saved file would decode it wrongly. Download saves the output as <name>.formatted.xml, or formatted.xml for pasted XML.

What well-formed means

A well-formed document follows the syntax of XML 1.0 (Fifth Edition). It has exactly one root element. Every tag is closed, and closed in the order it was opened. An element has no attribute twice, and every attribute value is quoted and holds no <. Names use only the characters XML allows. Every & starts a reference: one of the five predefined entities, a character number, or an entity the DOCTYPE declares. An XML declaration, if present, comes first. Text holds no ]]>, and no characters outside those XML allows. The tool checks all of these.

When the DOCTYPE points to an external DTD or uses parameter entities, the entities it declares may live in files the tool does not load. References to undeclared entities are then not checked, and a note says so. The exception is standalone="yes", which promises that no external declarations matter, so they are checked.

Namespace prefixes follow a separate specification. A prefix such as soap: used without an xmlns:soap declaration in scope is shown as a warning: the document is well-formed, but namespace-aware tools reject it.

In mixed content, an element holding text beside child elements, such as a paragraph with a bold word, the whitespace is part of the text. Indenting it would add spaces and line breaks to what the document says. So mixed content stays on its original lines, in both formats. The same holds for text-only elements, CDATA sections, and elements marked xml:space="preserve", until a descendant marked xml:space="default". Elements nested more than 1,000 deep are refused.

Examples

An RSS item, pretty-printed

Input
<rss version="2.0"><channel><title>Corner news</title><item><title>Rates change in March</title><link>https://example.com/rates</link><description>New rates <b>from 1 March</b> &amp; what they mean.</description></item></channel></rss>
Pretty, default options
<rss version="2.0">
  <channel>
    <title>Corner news</title>
    <item>
      <title>Rates change in March</title>
      <link>https://example.com/rates</link>
      <description>New rates <b>from 1 March</b> &amp; what they mean.</description>
    </item>
  </channel>
</rss>

Each element goes on its own line. The description holds text beside a <b> element, so it stays as written, and &amp; stays a reference.

A SOAP envelope, minified

Input
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope">
  <soap:Body>
    <m:GetPrice xmlns:m="https://example.com/prices">
      <m:Item>Apples</m:Item>
    </m:GetPrice>
  </soap:Body>
</soap:Envelope>
Minified
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope"><soap:Body><m:GetPrice xmlns:m="https://example.com/prices"><m:Item>Apples</m:Item></m:GetPrice></soap:Body></soap:Envelope>

The indentation between tags goes. The namespace prefixes and declarations stay.

A tag closed with the wrong name

Input
<note>
  <to>Asha</to>
  <from>Ravi</form>
</note>
Message
Not well-formed: Expected </from> to close <from> from line 3, found </form> (line 3, column 13)

The message names the tag that is still open and where it opened. Line 3 is marked in the input.

Frequently asked questions

Well-formed is about syntax: any XML parser can read the document. Valid also means it follows a schema, a DTD or an XML Schema (XSD) that says which elements and attributes may appear where. This tool checks well-formedness only; it does not validate against a DTD or XSD.
It is mixed content, a text-only element, or marked xml:space="preserve". The whitespace there is part of the text, so changing it would change what the document says. Elements that hold only other elements are indented.
References are kept as written, so the output says what the input said, and the DOCTYPE that declares them still applies. Never expanding them also makes entity bombs such as “billion laughs” harmless: a few lines that would expand to gigabytes stay a few lines.

See also