Validate XML Against XSD
: Validate an XML document against an XSD schema with libxml2, every error by lineValidate XML Against XSD
Paste an XML file and the XSD it should follow, and every place where the document breaks the schema is listed by line. The check is libxml2, the library behind xmllint --schema, compiled to WebAssembly and running in this tab, so neither file is uploaded.
How to validate XML against an XSD
- Paste the document into the
XML documentpane and the schema intoXSD schema, or pressOpen fileon either pane (or drop a file on it) to read a file from your disk.ExamplesoffersValid example, an invoice and its schema that pass, andInvalid example, the same schema with a document that breaks it in four places. - The check runs a moment after you stop typing. The engine starts loading with the page; if you are faster, a notice says
Loading the libxml2 engine…until it is ready, which only takes a while on the first visit. - Read the verdict at the top of the
Reportpane:Valid against the schema,Not validwith an error count,Not well-formed XML, orSchema errors, in which case the document was not checked at all. - Click a finding to jump to its line: document errors select the line in
XML document, schema errors the line inXSD schema. - Press
Copyfor the whole report as plain text, orDownloadto save it.
Example
A schema for an order, and a document with three mistakes: no id attribute, a word where a positive integer belongs, and an element the schema does not declare. Line numbers count from the top of the XML document; this is the text Copy gives.
<!-- schema.xsd -->
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="order">
<xs:complexType>
<xs:sequence>
<xs:element name="sku" type="xs:string"/>
<xs:element name="qty" type="xs:positiveInteger"/>
</xs:sequence>
<xs:attribute name="id" type="xs:string" use="required"/>
</xs:complexType>
</xs:element>
</xs:schema>
<!-- document.xml -->
<order>
<sku>X-1</sku>
<qty>two</qty>
<gift>yes</gift>
</order>document is not valid against the schema — 3 errors
[x] line 1 — Element 'order': The attribute 'id' is required but missing.
[x] line 3 — Element 'qty': 'two' is not a valid value of the atomic type 'xs:positiveInteger'.
[x] line 4 — Element 'gift': This element is not expected.Details
- The schema is compiled before anything else. If it is not well-formed or libxml2 refuses it, the report shows those schema errors with their lines in the schema, each with the same plain-English
WhyandFixnotes as the XSD Validator gives, and says the document was not checked. A schema error usually means every document would fail, so it comes first. - Next the document is parsed. A syntax problem such as an unclosed tag is reported as not well-formed, with line and column, and no schema rule is checked until it is fixed.
- Validation errors are libxml2's own messages, one per finding, with the line of the element that broke the rule. libxml2 does not report a column for these, so the line is what you get. It keeps going after the first error, so one run usually shows them all.
- The root element must match a global
xs:elementin the schema, and its namespace must equal the schema'stargetNamespace. A mismatch shows up asNo matching global declaration available for the validation root. - Entities declared in the document's own DTD are expanded before validation. External entities and DTD subsets are never loaded, and the network is never used.
Limits
- XML Schema 1.0 only, which is what libxml2 implements. XSD 1.1 features such as
xs:assertandxs:alternativeare rejected as schema errors. - One schema file.
xs:include,xs:importandxs:redefineare never fetched from theirschemaLocation; the report lists each one it skipped. An unresolvable include stops the compile, and types from a missing import show up as unresolved references. Merge the parts into one schema to validate against them. - The
xsi:schemaLocationandxsi:noNamespaceSchemaLocationhints in the document are ignored: the schema in theXSD schemapane is the one used. - A document of many megabytes is checked in the background, so the tab stays responsive: when the two panes together pass 2,000,000 characters, the check waits for you to press
Check, says which step it is on, andCancelstops it. The limit is libxml2's memory, at most 2 GB in the tab: about 200 MB of typical XML is the most it holds, and a larger file gets a message to split it. - No DTD, RELAX NG or Schematron validation.
FAQ
Why does it say no matching global declaration for the validation root?
The document's root element is not declared at the top level of the schema, or it is in a different namespace. If the schema has targetNamespace="urn:x", the root needs xmlns="urn:x" (or a prefix bound to it), and the element has to be a direct child of xs:schema.
Why are there schema errors when I only wanted to check my XML?
libxml2 cannot validate against a schema it cannot compile, so a broken XSD stops the run. Fix the schema errors listed, or check the schema on its own with the XSD Validator.
Does it use the schema named in xsi:schemaLocation?
No. Nothing is fetched, so the only schema used is the one you paste or open in the XSD schema pane.
Runs entirely in your browser. Nothing is uploaded.