1. Home
  2. Blog
  3. How to Format and Validate XML: Well-Formed vs Valid Explained
XML Toolkit

How to Format and Validate XML: Well-Formed vs Valid Explained

Published 28 October 2026 · 7 min read

XML still runs a lot of the world: RSS feeds, sitemaps, invoices, bank files, Office documents, Android layouts and countless configuration files. It’s also unforgiving. A single unclosed tag or a stray &, and the program reading it stops with an error.

This guide shows how to format XML so it’s readable, the difference between well-formed and valid, how to validate against an XSD schema, and how to fix the six errors behind most broken XML, using the free XML Formatter and XML Validator.

Formatting XML

XML often arrives on a single line, generated by a program for other programs. Formatting adds line breaks and indentation so you can see the structure:

<catalog><book id="bk101"><title>XML Basics</title><price>29.95</price></book></catalog>
<catalog>
  <book id="bk101">
    <title>XML Basics</title>
    <price>29.95</price>
  </book>
</catalog>

A good formatter changes only the whitespace between tags. Element names, attributes, text, comments and CDATA sections stay exactly as they were, and text that mixes words and tags (like <p>Hello <b>world</b></p>) is left untouched, because its spaces are part of the content.

How to format XML, step by step

  1. Open the XML Formatter and paste your XML into XML input, click Open file, or drop an .xml file onto the box. Sample loads an example.
  2. Watch the line under the box: it says ✓ This is well-formed XML, or shows the first mistake with its line and column.
  3. Under Indentation, choose 2 spaces, 4 spaces, Tab, or One line (minified) to squeeze it back onto one line. Tick Write empty elements as <tag/> to shorten empty elements, or untick Keep comments to drop comments.
  4. Click Format XML, then copy or download the result.

Well-formed vs valid: what’s the difference?

These two words are often mixed up, but they mean different things:

  • Well-formed XML follows the rules of the XML language itself: every tag is closed and properly nested, there is exactly one root element, attribute values are quoted, and special characters are escaped. Every XML file must be well-formed, or nothing can read it.
  • Valid XML is well-formed and follows a schema: a description of which elements and attributes are allowed, in which order, and what kind of values they hold. For example, “every book needs an id, and the price must be a number”.

The most common schema language is XSD (XML Schema). Schemas are used wherever two systems exchange XML and must agree on its exact shape, such as e-invoices, bank transfers and product feeds.

How to validate XML against an XSD schema

  1. Open the XML Validator and add your XML on the left.
  2. Paste your schema into XSD schema (optional) on the right, or open an .xsd file. Leave it empty to check only that the XML is well-formed.
  3. Click Validate XML. A green report means the XML matches the schema. Otherwise every problem is listed with its line, such as a missing required attribute or a price that isn’t a number, and Go to line jumps straight to it.

The schema check runs in your browser with libxml2, the standard XML library used by many programs, so its messages are in English. Schemas that import other files need those definitions copied into one schema first.

The 6 most common XML errors and how to fix them

1. A tag that is never closed

Every opening tag needs a closing tag, or must close itself, like <cover/>.

<!-- wrong -->
<title>XML Basics

<!-- right -->
<title>XML Basics</title>

2. Tags closed in the wrong order, or with different letter case

Tags must close in the reverse order they opened. XML is also case-sensitive: <Title> and </title> don’t match.

<!-- wrong -->
<b><i>Important</b></i>
<Title>XML Basics</title>

<!-- right -->
<b><i>Important</i></b>
<title>XML Basics</title>

3. An unescaped ampersand or less-than sign

In text, & must be written &amp; and < must be written &lt;. This is the most common error in XML made by hand.

<!-- wrong -->
<company>Smith & Sons</company>

<!-- right -->
<company>Smith &amp; Sons</company>

4. Attribute values without quotes

HTML sometimes lets you leave them out; XML never does. Use double or single quotes.

<!-- wrong -->
<book id=101>

<!-- right -->
<book id="101">

5. More than one root element

An XML document has exactly one outermost element. Wrap repeated elements in a parent.

<!-- wrong -->
<book>First</book>
<book>Second</book>

<!-- right -->
<books>
  <book>First</book>
  <book>Second</book>
</books>

6. HTML entities such as &nbsp;

XML only knows five named entities: &amp;, &lt;, &gt;, &quot; and &apos;. Write others as character numbers, such as &#160; for a non-breaking space.

<!-- wrong -->
<price>10&nbsp;EUR</price>

<!-- right -->
<price>10&#160;EUR</price>

The formatter and the validator recognise each of these mistakes and explain them in plain words, with the line and column.

More you can do with XML

Is my XML uploaded?

No. Formatting and validating, including the schema check, happen in your browser, so your XML and your schema never leave your device.

Frequently asked questions

Do I need a schema to validate XML?

No. Without a schema, the validator checks that the XML is well-formed, which catches most errors. A schema adds checks on structure and values.

Why is &nbsp; reported as an error?

Because it’s an HTML entity, not an XML one. Use &#160; instead, or declare the entity in the document’s DOCTYPE.

Does formatting change my data?

No. Only the whitespace between tags changes. Text, attributes, comments and CDATA stay exactly as they were.