XML Validator
Check XML for well-formedness errors with exact line and column, and validate it against an XSD or DTD
Paste XML to check it
Reports the exact line and column of the first error, as XML parsers must stop there. Fix it and the next one appears.
Validate against a schema (optional)
The language is detected automatically. If your XML has a DOCTYPE with <!ELEMENT> declarations, it is checked against them even when this box is empty. External DTDs and schema files are never downloaded: paste their text here.
Result
Find out why a parser rejects your XML
Paste a document and the tool tells you whether it is well-formed, and when it is not, shows the exact line and column of the first error, the source line with a caret under it, and the usual cause. Add an XSD or a DTD and it also checks the order of elements, how many times each may appear, required attributes and the values themselves. Everything runs in your browser on a parser written for this page, so the result does not depend on which browser you use, and nothing is uploaded, including DTDs and entities, which are never downloaded.
Well-formed, valid, or both?
These two words are often used loosely, but they answer different questions. Well-formed means the text obeys the XML grammar: a single root element, tags that close in the reverse order they opened, quoted attribute values, special characters escaped, no illegal control characters, and namespace prefixes that are declared. Valid means the document additionally matches a schema (an XSD or a DTD) that fixes which elements and attributes may appear, in which order and with which values. A valid document is always well-formed, never the other way round.
Why does the tool show one well-formedness error at a time? The XML specification requires a parser to stop at the first fatal error, because from that point the structure is unreliable and later messages would mostly be consequences of the first one. A forgotten closing tag, for instance, makes every following tag look wrong. Fix the reported error and the document is re-checked as you type, so the next one appears at once. Schema violations are different: once the document is well-formed, every violation can be found independently, so they are all listed (up to 100).
The errors you will actually meet, and their causes
- Closing tag does not match.
<item>text</items>. The message names the element that is open and where it was opened. Typos are flagged as such, and when the closing tag matches an element further up, the elements left open in between are listed. XML is case-sensitive:<Item>and</item>differ. - Unescaped ampersand. Typically a URL with several parameters
(
?a=1&b=2, which must be written?a=1&b=2) or a name such as AT&T. A CDATA section is the alternative for text with many special characters. - An entity that is not defined. XML predefines only
< > & " '. HTML names like ,©or€do not exist, so use ,©,€. The tool suggests the number when it recognises the HTML name. - Unquoted or valueless attributes.
<a id=1>and<input disabled>are legal HTML but not XML. Writeid="1"anddisabled="disabled". - Extra content after the root. Two sibling top-level elements are not a document. Wrap them in one parent element.
- Undeclared namespace prefix.
soap:Envelopewithoutxmlns:soap="…"in scope. Common after copying a fragment out of a larger document whose root held the declaration. - The XML declaration is not first.
<?xml …?>is only legal at the first character; a blank line or space before it is an error. A byte order mark is the one allowed exception. - Illegal characters. Control characters such as U+0001 are forbidden in
XML 1.0, even as references like
. They usually arrive through copy and paste or a bad export; the report gives the code point and position. - Comment and CDATA rules.
--may not appear inside a comment, and]]>may not appear in ordinary text.
Validating against an XSD
Paste a schema in the optional box and the document is checked against the real content
model, not just element names. Messages say what was expected: for example
<total> is not allowed here inside <order>; expected <customer> when
children are in the wrong order, because in an xs:sequence order matters.
Checked: sequence, choice and xs:all; minOccurs and
maxOccurs; element wildcards (xs:any); target namespaces and elementFormDefault;
extension and restriction of complex types; groups and attribute groups; required, optional,
prohibited and fixed attributes; mixed content; xsi:nil; ID uniqueness and IDREF
targets; the built-in types (integers with their ranges, decimal, boolean, dates and times with
real calendar checks, durations, base64Binary, hexBinary, language, NCName…); and the facets
enumeration, pattern, length, minLength, maxLength, minInclusive, maxInclusive, minExclusive,
maxExclusive, totalDigits, fractionDigits and whiteSpace, including list and union types.
Not checked, and listed when the schema uses it: identity constraints
(xs:key, xs:keyref, xs:unique), substitution groups,
xsi:type overrides, types from files loaded with xs:import or
xs:include (paste everything into one schema), XSD 1.1 assertions, and the parts of
the XSD regular-expression dialect that JavaScript cannot express. A document that passes
here can still be rejected by a stricter validator for one of these reasons, which is why they
are reported instead of ignored.
Validating against a DTD
A DTD describes elements with content models such as (title, author+, (price | free)?)
and attributes with types and defaults. If your document has a DOCTYPE with an internal
subset containing <!ELEMENT> declarations, it is validated against it
automatically. For an external DTD, paste its text in the schema box: the file named in the
DOCTYPE is never downloaded.
The checks are the validity constraints that matter in practice: the root element matches the
DOCTYPE name; every element is declared; children match the content model (order, choice,
?, *, +); EMPTY elements are empty; mixed
content allows only the listed names; text is not allowed in element-only content;
attributes are declared, required ones are present, enumerated values and
#FIXED values match, and ID and IDREF values are unique and resolve. Parameter
entities inside the DTD are not expanded; declarations that depend on them are skipped and
the report says so.
Encoding, BOM and why a declaration can lie
A browser text box holds characters, not bytes, so there is one thing this tool cannot verify:
that the bytes of your file match the encoding in its declaration. A file that says
encoding="UTF-8" but was saved as Windows-1252, a typical outcome with smart quotes
copied from a word processor, looks perfectly fine once pasted and still fails in a strict
parser with an "invalid byte sequence" error. If you see that, re-save the file as UTF-8.
When a declaration names another encoding the report adds a warning to remind you.
UTF-8 is the default when there is no declaration, and a byte order mark at the start is accepted. Characters outside the Basic Multilingual Plane, such as emoji, are allowed, while unpaired surrogates and control characters are reported with their code point and position.
Safety: DOCTYPE, entities and XXE
A DOCTYPE can declare entities, including external ones that point to a file or a URL. A parser that resolves them can be made to read local files or contact internal servers: this is the XXE (XML External Entity) attack. This tool never fetches anything; it shows a warning instead, so you can see the risk in a document without exposing yourself to it.
It also defends against "billion laughs" documents, where entities nested ten deep expand to gigabytes: expansion is capped, the document is rejected with a clear message, and the page stays responsive. In your own code, turn off DTD processing and external entity resolution in the parser (it is a one-line setting in most libraries) unless you really need them.
Code Examples
Check well-formedness from the command line with xmllint
printf '<root>\n <item>text</items>\n</root>\n' > bad.xml
xmllint --noout bad.xml
echo "exit status: $?"Output:
bad.xml:2: parser error : Opening and ending tag mismatch: item line 2 and items
<item>text</items>
^
exit status: 4Validate against an XSD with xmllint
cat > order.xsd <<'EOF'
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="order">
<xs:complexType>
<xs:sequence>
<xs:element name="customer" type="xs:string"/>
<xs:element name="total" type="xs:decimal"/>
</xs:sequence>
<xs:attribute name="id" type="xs:int" use="required"/>
</xs:complexType>
</xs:element>
</xs:schema>
EOF
printf '<order id="7"><total>9.90</total><customer>Ana</customer></order>\n' > order.xml
xmllint --noout --schema order.xsd order.xmlOutput:
order.xml:1: Schemas validity error : Element 'total': This element is not expected. Expected is ( customer ).
order.xml fails to validatePython: validate with lxml and read every error
from lxml import etree
schema = etree.XMLSchema(etree.parse("order.xsd"))
doc = etree.parse("order.xml") # raises XMLSyntaxError if not well-formed
if not schema.validate(doc):
for error in schema.error_log:
print(f"line {error.line}: {error.message}")JavaScript: a well-formedness check with DOMParser (error text differs per browser)
function isWellFormed(xml) {
const doc = new DOMParser().parseFromString(xml, "application/xml");
// Chrome and Firefox put the error in a <parsererror> element, in different formats.
return doc.getElementsByTagName("parsererror").length === 0;
}
isWellFormed("<a><b/></a>"); // true
isWellFormed("<a><b></a>"); // falseTurn off external entities in your own parser (Java)
DocumentBuilderFactory f = DocumentBuilderFactory.newInstance();
f.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
f.setXIncludeAware(false);
f.setExpandEntityReferences(false);Standards & Specifications
- W3C Extensible Markup Language (XML) 1.0, Fifth Edition — The well-formedness constraints, character classes, DOCTYPE and entity rules this validator enforces
- W3C Namespaces in XML 1.0, Third Edition — Prefix declaration, the reserved xml and xmlns prefixes and expanded attribute names
- W3C XML Schema Part 1: Structures — Content models, particles and attribute uses checked in XSD validation
- W3C XML Schema Part 2: Datatypes — Built-in types and facets such as pattern, enumeration and fractionDigits
Frequently Asked Questions
Why does the validator show only one error?
The XML specification says a parser must stop at the first fatal well-formedness error, because after it the structure of the document is unreliable and later messages would often be false alarms. Fix the reported error and the next one appears immediately, since the document is re-checked as you type. Schema problems (XSD or DTD) are different: once a document is well-formed, every violation found is listed, up to 100.
What is the difference between well-formed and valid XML?
Well-formed means the text follows the XML grammar: one root element, properly nested and closed tags, quoted attributes, escaped special characters and declared namespace prefixes. Valid means the document also matches a schema (an XSD or a DTD) that says which elements and attributes may appear, in which order, how many times and with which values. Valid implies well-formed, never the other way round. Without a schema this tool checks well-formedness and the namespace rules.
How do I fix "the entity nbsp was referenced, but not declared" or an unescaped ampersand?
XML predefines only five entities: < > & " and '. HTML names such as , © or € do not exist in XML, so write the number instead ( , ©, €) or put the character itself in a UTF-8 file. A bare & is the other common cause, typically in a URL like ?a=1&b=2 or in a name like AT&T: write & there, or wrap the text in a CDATA section.
Does it validate against an XSD or a DTD?
Yes. Paste the schema and the document is checked against real content models: the order of child elements, minOccurs and maxOccurs, choices, xs:all, wildcards, required and unknown attributes, built-in and restricted types (enumerations, patterns, lengths, ranges, digits), ID and IDREF. For XSD it covers a documented subset of XSD 1.0 and lists what it skipped, such as xs:key and xs:unique, substitution groups, xsi:type, and types in files loaded with xs:import or xs:include. A DOCTYPE with internal declarations is used automatically.
Why does my file pass here but fail in my application, or the other way round?
Three usual reasons. The application may check against a schema this tool does not have or uses XSD 1.1 features. Pasted text is already decoded, so a file saved in an encoding that disagrees with its declaration (for example Windows-1252 smart quotes in a file that claims UTF-8) passes here and fails in a strict parser. And some parsers are lenient: browsers' DOMParser reports errors differently from each other, and HTML parsers accept what XML rejects. This tool follows the XML 1.0 and Namespaces rules, which is what strict parsers apply.
What does "namespace prefix is not declared" mean?
An element or attribute such as soap:Envelope uses a prefix that no xmlns:soap="…" declaration in scope defines. Declarations are inherited from ancestors, so the fix is usually to add xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" to the root or to the element that starts the fragment. A fragment copied out of a larger document is the classic trigger, because the declarations stayed on the original root.
Why is my XML declaration rejected when it looks fine?
The declaration <?xml version="1.0"?> is only legal at the very first character of the document. A blank line, a space or a comment before it is an error, because after any content the text <?xml is a reserved processing instruction name. A byte order mark at the start is the one thing that may precede it. Also keep the pseudo-attributes quoted and in the order version, encoding, standalone.
Is my XML uploaded, and are external DTDs or entities fetched?
Nothing is uploaded: parsing and validation run in your browser, in a Web Worker for large documents. External DTDs, external entities and schemaLocation files are never downloaded, so a DOCTYPE pointing at a URL or a SYSTEM entity such as file:///etc/passwd cannot read anything. The tool shows a warning that it was skipped. In your own code, disable DTD and external-entity processing to avoid XXE attacks.
How large a document can it handle?
Files up to 25 MB can be opened. Documents above roughly 400 KB are validated in a background Web Worker, so the page stays responsive. As a guide, a parse of a few megabytes takes a fraction of a second on a laptop, and expansion of entities is capped to stop "billion laughs" documents before they exhaust memory.
Can I validate a fragment or several root elements?
No: a document must have exactly one root element, and text before or after it is an error. To check a fragment, wrap it in a temporary root such as <root>…</root>. If the fragment uses namespace prefixes, declare them on that temporary root as well.