Skip to content

How to Validate XML and Fix Well-Formedness Errors

By Durim Hoti · Last updated

XML parsers are strict by design: at the first syntax error they stop and refuse the whole document. A sitemap, an RSS feed or a config file with one wrong character can therefore be ignored completely by the program that reads it.

Validate XML step by step

  1. Open the XML validator.
  2. Paste the XML or click Open file. The result appears right away.
  3. If it says Invalid XML, read the message, the line and the column. The line is shown with a ^ under the problem.
  4. Click Go to error, fix the spot and check the result again; the validator shows one error at a time.
  5. When it's well-formed, make it readable with the XML formatter if needed.

Example: a sitemap with an ampersand in a URL

sitemap.xml
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
  <url>
    <loc>https://example.com/</loc>
  </url>
  <url>
    <loc>https://example.com/search?q=shoes&page=2</loc>
  </url>
</urlset>

The validator reports: line 7, column 44, “&” must be written as &amp; (or start an entity like &lt;).

The XML validator with the sitemap. Line 7 is highlighted in red. The result says Invalid XML, Error on line 7, column 44: “&” must be written as & (or start an entity like <), with a caret under the ampersand in the search URL.
Line 7, column 44: the & between the two URL parameters.

The fix is to escape the ampersand. The URL itself doesn't change: programs that read the sitemap turn &amp; back into &.

sitemap.xml (fixed)
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
  <url>
    <loc>https://example.com/</loc>
  </url>
  <url>
    <loc>https://example.com/search?q=shoes&amp;page=2</loc>
  </url>
</urlset>

Common XML errors and the messages

We ran each snippet through the validator; this is the first error it reported.

Mismatched tag

XML
<note>
  <to>Tove</To>
</note>

Line 2, column 11: End tag </To> doesn't match the open element <to> (line 2). Close <to> first.

Unclosed tag

XML
<note>
  <to>Tove
</note>

Line 3, column 1: End tag </note> doesn't match the open element <to> (line 2). Close <to> first.

Two root elements

XML
<a>1</a>
<b>2</b>

Line 2, column 1: Only one root element is allowed. Wrap the elements in a single parent element.

Attribute without quotes

XML
<book id=bk101>
</book>

Line 1, column 10: The value of “id” must be in quotes.

HTML entity

XML
<p>Price:&nbsp;5</p>

Line 1, column 10: Undefined entity &nbsp;. XML only defines &lt; &gt; &amp; &quot; &apos; — use a character reference like &#160; instead.

A < sign in text

XML
<expr>a < b</expr>

Line 1, column 10: Invalid element name: names can't start with “ ”.

Something before the declaration

XML

<?xml version="1.0"?>
<a/>

Line 2, column 1: The XML declaration <?xml … ?> must be at the very start of the document, before any whitespace.

The message for a < b needs a word of explanation: the parser reads < as the start of a tag, then finds a space where the tag name should be. Write &lt;, or wrap text with many special characters in a CDATA section, which the validator accepts:

XML
<expr><![CDATA[a < b && c]]></expr>

Common problems

The error points to the end of the file

A tag opened somewhere earlier was never closed, so the parser only notices at a later closing tag. The message names the open element and its line; start looking there.

The XML is valid here but rejected elsewhere

The other program probably checks a schema: an element name, order or value it doesn't expect. Check its documentation, for example the sitemap protocol for sitemaps.

Strange characters in the file

Save the file as UTF-8 and keep the encoding in the XML declaration in line with how the file is saved.

What to do next

Working with JSON as well? See how to fix JSON errors, and for config files JSON vs YAML. To turn XML into JSON, use the XML to JSON converter.

FAQ

What is the difference between well-formed and valid XML?
Well-formed XML follows the syntax rules every XML parser enforces: one root element, closed and correctly nested tags, quoted attributes, escaped special characters. Valid XML is also checked against a schema (XSD, DTD or RELAX NG) that defines which elements are allowed. Our validator checks well-formedness, not schemas.
Why does an & in a URL break my XML?
In XML, & starts an entity such as &lt;. A bare & followed by other text is an error, so a link like ?q=shoes&page=2 must be written as ?q=shoes&amp;page=2. Programs reading the XML turn &amp; back into &.
Can I use &nbsp; or &copy; in XML?
Not unless a DTD defines them. XML only knows &lt;, &gt;, &amp;, &quot; and &apos;. Use numeric references instead, for example &#160; for a non-breaking space.
Does the validator check my XML against an XSD schema?
No. It checks well-formedness, which every parser requires first. For schema validation, use a schema-aware tool or your code editor's XML support.
Is my XML uploaded?
No. The XML is parsed in your browser.

Try it and keep reading