XML and XSD Validator
Operator: Abdulla Al Fahad. How tools are built and checked
An XML feed arrives with a schema that imports a second schema, and the usual online validators accept one pasted file. This page takes the whole set: add order.xsd and types.xsd together and the import is resolved between them, never fetched from the address written inside the schema.
It checks three things in one place: that the document is well-formed, that it is valid against an XSD 1.0 schema or a DTD, and that a given XPath 1.0 expression selects what you expect. Every error is listed with its line, and a click scrolls the document view to it.
Key features
- Well-formedness check with the parser's own message, line and column
- XSD 1.0 validation against a main schema plus any number of imported or included files, up to 40 files of 6 MB each
- DTD validation from an internal subset or an uploaded .dtd
- Document view with line numbers, a highlighted error line and the column marked
- Pretty-print through xmllint's formatter, plus an XPath 1.0 tester that takes namespace prefixes from the root element
How imports, entities and encodings are handled
Validation runs in libxml2 2.13 compiled to WebAssembly (xmllint-wasm 5.3), inside a worker that is served from this site and stopped after 90 seconds. It supports XSD 1.0; a schema that relies on 1.1 features such as xs:assert is reported as a problem in the schema, not silently skipped.
Files are matched by name. For every schemaLocation in your schemas, the page looks for an added file with the same base name, including when the location is a web address or a folder path, and points the reference at it. Any reference with no matching file is listed, and libxml2 then reports that the import failed to load. Nothing is requested over the network.
Documents are read as UTF-8, UTF-16 with a byte-order mark, or the encoding named in the XML declaration. When another encoding is decoded, the declaration is rewritten to UTF-8 for the check only and a note says so.
The document limit is 24 MB, the DTD checker reads documents up to 12 MB, and the XPath tester up to 6 MB. Pasted text is limited to about 2 MB; use the file picker for anything larger.
How it works
A fresh worker is started for each run, because xmllint ends its process when it finishes; Cancel simply terminates it. libxml2's own limit on entity expansion stays on: a billion-laughs document stops with the message Maximum entity amplification factor exceeded instead of using up the tab's memory. External entities are never loaded, since the in-memory file system holds only the files you added.
The xmllint command line has a --valid option, but the WebAssembly wrapper used here does not expose it, so DTD rules are checked by code written for this page after libxml2 has confirmed the document is well-formed. That check runs in its own worker and stops after 60 seconds. It covers content models for EMPTY, ANY, mixed and element content, attribute types, defaults, #REQUIRED and #FIXED, enumerations, ID uniqueness and IDREF targets. Entities are not expanded for that check.
When to run it
- An integrator gets a 12 MB catalog feed and a schema that imports a code list. Adding both files turns a failed import into 3 genuine content errors, each with a line number.
- A developer pastes a SOAP response, sees Opening and ending tag mismatch on line 2 with the column marked, and finds a stray closing tag within seconds.
- A reviewer runs //s:line[s:qty > 50]/s:sku on the example order and gets the single node tea-120 with its path.
Running a check
- Drop an XML file or paste it; use Example with XSD to try the sample set.
- Add the schema files, tick the main one and set Check to Against an XSD 1.0 schema.
- Press Validate and read the problem list; choose Show in document to jump to a line.
- Fix the first error and validate again until the verdict says valid.
- Switch to XPath, enter an expression and press Evaluate.
Example
Example with XSD: order.xml checked against order.xsd, which includes types.xsd by web address
<line><sku>tea-120</sku><qty>120</qty></line> (lines 9 and 10 of order.xml)
SKU pattern [A-Z]{3}-[0-9]{3}, quantity maximum 99Problems listed
line 9: Element sku: the value 'tea-120' is not accepted by the pattern '[A-Z]{3}-[0-9]{3}'
line 10: Element qty: the value '120' is greater than the maximum value allowed ('99')Tips for best results
- A default namespace has no prefix in XPath 1.0. Give it one in the namespace box, as in s=urn:example:shop, and write s:line.
- Fix the first error before the rest; one missing element often causes the lines after it to be reported too.
- Pretty-print leaves mixed content alone, so inline markup in prose keeps its spaces.
Limits
Only XSD 1.0 is validated; XSD 1.1 assertions and type alternatives are not supported.
A schema that needs a file you did not add fails as a schema problem, and the document is then not checked at all.
The XPath tester uses the browser's engine on a copy of the document and refuses documents that declare entities or exceed 6 MB.
RELAX NG and Schematron are not offered, although the engine could run them.
Common errors and how to fix them
- "Failed to load the document ... for inclusion"
- A schemaLocation points to a file that was not added. The warning above the list names it; add that file with the same base name.
- "The schema could not be used"
- The schema itself is invalid or uses a feature outside XSD 1.0. The listed problems carry the schema file name and line.
- "There is no DTD to validate against"
- The document has no internal subset and no .dtd was added. Add the DTD file, selected as the main one.
What the DTD check covers, and where it stops
Content models are matched as automata, so (a|b)*, (title, para+) and nested groups behave as the DTD says, and a mismatch names the unexpected child and what was expected there. Attribute rules are checked on the raw attribute text after the normalisation the DTD type calls for: tokenised types such as NMTOKEN, ID and IDREF have their whitespace collapsed first.
Three things are outside it. Entities are not expanded, so markup that exists only inside an entity is not checked. Parameter entities are expanded up to 2 MB in total, and a DTD that grows past that is refused. If one content model needs an unreasonable amount of work for the document, the page stops checking content models at that element and says where, rather than freezing the tab.
Frequently asked questions
How is this different from the XML to CSV tool here?
That tool flattens repeating elements into rows and assumes the file is already sound. This one tells you whether the file is sound and which rule it breaks.
Why can't it fetch the schema from the web address in my file?
Nothing here makes a network request on behalf of a document, because a hostile document could use that to probe addresses. Download the schemas and add them; the references are matched by file name.