XML guide · 5 min read
How to validate XML against an XSD schema
Three ways to validate XML against an XSD: online tool, command line (xmllint), and programmatically in Python, Java, and Node.js. With error interpretation.
Last updated:
[object Object]
[object Object]
1 Option 1 — Online (zero install)
[object Object]
[object Object]
2 Option 2 — Command line with xmllint
[object Object]
[object Object][object Object]
[object Object]
[object Object]3 Option 3 — Python with lxml
[object Object][object Object]
4 Option 4 — Java with javax.xml.validation
[object Object][object Object]
5 Option 5 — Node.js with libxmljs2
[object Object][object Object]
6 Reading XSD error messages
[object Object]
- [object Object]
[object Object]
Frequently asked questions
What's the difference between well-formed and valid XML?
Well-formed XML is syntactically correct (every tag closed, one root element, proper escaping). Valid XML is well-formed *and* conforms to a schema (XSD, DTD, or RelaxNG). A file can be well-formed but invalid.
My XSD uses `xs:import` for another namespace — how do I validate?
Make sure both XSDs are accessible (local filesystem or HTTP). `xmllint` uses the `schemaLocation` attribute; you may need to pass `--xinclude` or set up a catalog file for complex imports.
Is DTD validation different?
Yes. DTD (Document Type Definition) is older and less expressive. `xmllint --valid` validates against a DTD declared inside the XML. Most modern contracts use XSD instead.
Can I generate an XSD from an XML document?
Yes — tools like `xsd.exe` (Windows), `Trang`, or `xmlspy` can infer an XSD from a sample XML. The result is usually a starting point you'll need to tighten by hand (making optional elements required, etc.).
