Well-formed and valid are not the same
Two checks have the name validation. Well-formed means the document follows the syntax rules of XML: tags match, attributes have quotes, special characters are escaped, and there is one root element. Valid means it also follows a schema, such as an XSD or a DTD, which says which elements and attributes are allowed. This tool checks that your XML is well-formed. It does not check a schema. A document that is not well-formed cannot be read by any XML program, so this is the first check to pass.
What is checked
- Every start tag has a matching end tag, in the right order, with the same name and case.
- There is exactly one root element, and nothing but comments and whitespace outside it.
- Attribute values are in quotes, and no attribute appears twice on an element.
- Names follow the XML rules for the first character and the later characters.
- The characters < and & are escaped in text, and every entity is either one of the five predefined ones, a valid number, or declared in the DOCTYPE.
- Comments, CDATA sections and processing instructions are written correctly.
- The XML declaration, when it is there, is at the very start and has a valid version, encoding and standalone value.
- Every namespace prefix, such as dc: in dc:creator, is declared with xmlns:dc. You can turn this check off.
- No characters that XML forbids, such as most control characters.
How errors are shown
XML parsers stop at the first serious error, because the rest of the document cannot be trusted after it. This tool keeps going where it can, so it lists problems that do not stop the reading, such as a duplicate attribute or a bare ampersand, together with the first error that does. Each one has its line and column, and the line of code is shown with a marker under the spot.
Common errors
| Message | Usual cause |
|---|---|
| Closing tag does not match | A tag was not closed, or the case differs (<Name> and </name>). |
| Bare ampersand | A text such as "Tom & Jerry" must be written Tom & Jerry. |
| Attribute value must be in quotes | id=42 instead of id="42". |
| Duplicate attribute | The same attribute appears twice in one tag. |
| Prefix is not declared | A name such as dc:title is used without xmlns:dc. |
| Text outside the root element | Extra text or a second root element after the first one closes. |
| XML declaration not at the start | A space, a blank line or a byte before the declaration. |
The summary
When the XML is well-formed you see the root element, the number of elements and attributes, the depth, the declared namespaces, and a table of the element names with how often each one is used and which attributes they carry. It helps you check that a file looks like you expect.
Fix bare ampersands
The most common error in hand-made XML is an unescaped &. A button replaces every bare ampersand with &, outside of comments and CDATA sections, and checks the result again.
Check against a schema
To check against an XSD from the command line, use xmllint --noout --schema schema.xsd file.xml. For well-formedness alone, xmllint --noout file.xml does the same as this page.
When the XML is good, tidy it with the XML formatter or the XML beautifier, shrink it with the XML minifier, or convert it with XML to JSON. The XML syntax guide lists the rules, and the HTML entities guide explains character references.