XML Validator
Check whether an XML document is well formed, see the first error with its line and column, and format it without changing its text.
Overview
A well formed document obeys the grammar of XML 1.0: every tag is closed by a matching one, attribute values are quoted, an ampersand starts a reference, and there is exactly one root element. That is what this tool checks. Whether a document is also valid, whether it matches a schema such as a DTD, an XSD or RELAX NG, is a further question that needs the schema, and this tool does not ask for one.
Web interface
Paste a document into the input; it is checked on every keystroke. The verdict under the input either says Well formed XML, with the size, the number of elements and attributes and the depth, or names the error and quotes its line with a caret under the column.
The formatted output takes an indent of 2, 4 or Tab, or Minified, and three switches: Keep comments, on by default, Sort attributes and Collapse whitespace. Structure shows the element tree, the counts and the namespaces the document declares. The dice in the corner of the input loads a random example, well formed or with a typical mistake.
Errors
Only the first error is reported, since XML gives a parser no reliable way to carry on after it. Lines and columns are counted from 1, in characters as a reader counts them.
| Mistake | Message |
|---|---|
<a>…</b> | </b> does not match <a>, which was opened on line 1 and is still open |
Fish & Chips | A bare & must be written as & |
without a DOCTYPE | There is no entity called nbsp; XML only defines amp, lt, gt, quot and apos |
� | � names U+0000, a character XML does not allow |
x="<", x="1" x="2" | A bare < is not allowed in an attribute value, write <, The attribute x appears twice on the same tag |
<a:b/> | The namespace prefix a on this tag is never declared; add xmlns:a="..." |
| two root elements | There can only be one root element, and <second> starts a second one |
a space before <?xml | Nothing may come before the XML declaration, not even whitespace |
]]> in text, -- in a comment | A literal ]]> is not allowed in content, write ]]>, A comment cannot contain a double hyphen |
The browser's own parser is asked as well, as a backstop. The tool's scanner accepts a handful of documents the browser rejects, around namespace URIs; for those the browser's message is shown, without the boilerplate of its error page. A test holds the scanner to never reject what the browser accepts.
Formatted output
Formatting changes the whitespace between elements and nothing else:
- text is written as it stands, including spaces at its start and end and no-break spaces;
- an element with mixed content, such as
<p>Hello <b>world</b>!</p>, is written in one piece, since new lines between its parts would change its text; - everything under
xml:space="preserve"is written exactly as it came; - CDATA sections, processing instructions and the DOCTYPE are kept.
The output is the document as the parser read it, so a character reference such as é
comes back as é, and an entity declared in the DOCTYPE comes back as its value. Checked on 1,440
formatted random documents with mixed content, CDATA, no-break spaces and xml:space, in every
indent and in Chrome, Safari's engine and Firefox: the text of each came back unchanged.
Collapse whitespace is the one switch that changes text on purpose: it turns runs of spaces and
line breaks into one space, outside xml:space="preserve".
Limits
Entities declared in a DOCTYPE are not read by the scanner, so once a DOCTYPE is present any entity name is accepted. Schemas are not checked, see Overview.
A document of 2.7 MB was checked and formatted in about half a second in Chrome. The check runs on every keystroke, so very large documents make typing in the input slow; paste them instead.
No API
There is no API for this tool. Validation runs in the browser; in a script,
xmllint --noout file.xml answers the same question.
Privacy
Nothing leaves the browser. The document is parsed on the page, nothing is stored, and the tool works with the network disconnected.