XML formatter and validator
Paste XML to format or minify it, and if it isn't well-formed, you'll see the line and column of the first error. It checks syntax only, not a DTD or XSD schema, and Format changes the layout but never the text inside elements. It runs in your browser; nothing you enter is uploaded.
Well-formed XML.
What well-formed means
XML is well-formed when it follows the syntax rules of the XML 1.0 standard. An XML parser rejects a file at the first rule it breaks, so one stray character is enough to make a feed or a config file fail to load. The checker looks for these errors and reports the first one in the file:
- a start tag with no end tag, or tags that overlap, as in
<b><i>text</b></i> - an end tag whose name differs in case, since
<Item>and</item>are two different names - more than one top-level element, since everything has to be inside one root element
- an attribute without quotes or without a value, or the same attribute twice in one tag
- a bare
<or&in text or in an attribute value, which have to be written as<and& - a namespace prefix, such as the
soapinsoap:Envelope, with noxmlns:soapattribute on that element or on one that contains it - an XML declaration,
<?xml version="1.0"?>, that isn't the very first thing in the file, even when only a blank line comes before it
Well-formed isn't the same as valid. A valid document also matches a schema, a DTD or XSD file that says which elements and attributes
are allowed where, and the tool doesn't check that. When a file has a DOCTYPE, the tool accepts any named entity, such as
©, because the DTD might define it, and it doesn't download the DTD to find out.
HTML habits that break XML
Browsers read HTML loosely, so markup copied from a web page often fails as XML. Each of these works in HTML and is an error in XML:
-
and the other named entities from HTML. XML knows only five,&,<,>,", and', so write a non-breaking space as or type the character itself. - empty elements without a slash, such as
<br>, which XML needs written as<br/> - attributes with no value, such as
<input disabled>, which XML needs written asdisabled="disabled" - unquoted attribute values, such as
width=100
To escape a block of text before you put it inside an element, use the HTML entity encoder. Its output is valid XML text too.
What Format and Minify change
Format puts each element on its own line and indents it by its depth. Minify removes that whitespace and puts the document on one line. Neither changes the text inside an element:
-
An element that holds text, such as
<p>Hello <b>world</b></p>, is copied exactly as written, tags inside it included, because a line break added there would become part of the text. Text that spans several lines keeps its original indentation, so it can look out of line with the new indentation. -
Comments, CDATA sections, processing instructions, the XML declaration, and the DOCTYPE stay as written. So do entity references:
&isn't turned into&. - Inside a tag, attributes get one space between them, and each value keeps its quotes.
- Windows line endings (CRLF) become LF, which is how every XML parser reads them anyway, and a byte order mark at the start is dropped.
Most programs that read XML as data ignore whitespace between tags, but in some places it's part of the content, such as inside the
<pre> element of XHTML. To keep an element's whitespace, add xml:space="preserve" to it, and Format and Minify copy that element
unchanged.
Finding the mistake from the error position
The line and column point at the start of the problem the message describes. For a mismatched end tag, that's where the parser noticed it,
which comes after the real mistake. In <a><b>text</a>, the error points at </a> and says
it expected </b>. The message also gives the line where <b> starts, and the missing
</b> belongs somewhere between that line and the error.
Checking XML on the command line
xmllint --noout data.xml # check; prints nothing if well-formed
xmllint --format data.xml # format with a 2-space indent
xmllint --noout --schema schema.xsd data.xml # validate against an XSD schema xmllint comes with the libxml2 library. macOS includes it, and on Debian and Ubuntu it's in the libxml2-utils
package. Unlike this tool, it can check a file against a schema.