XSD guide
How to validate XML against an XSD
Separate parsing from schema validation, load the complete schema set, and reduce validation errors to the smallest failing instance.
8 min read · Updated 2026-08-21
The short answer
First confirm that both the XML instance and schema documents are well-formed. Then validate the instance with the intended schema set, including every import and include, and fix the first structural error before interpreting the messages that follow it.Pass the parsing gate first
Schema validation never repairs malformed XML. A missing quote or unclosed tag prevents the validator from constructing the element tree whose names, order, values, and attributes the XSD constrains.
- Parse the instance document.
- Parse every XSD document in the schema set.
- Resolve imports and includes through an explicit catalog or controlled base URL.
- Only then run schema validation.
Match namespaces to declarations
The instance root's expanded name — namespace URI plus local name — selects a global declaration. A visually identical prefix is irrelevant, and xsi:schemaLocation is a hint rather than proof that the intended schema was loaded.
<product xmlns="urn:example:catalog" id="42">
<name>Notebook</name>
</product><xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="urn:example:catalog"
xmlns="urn:example:catalog"
elementFormDefault="qualified">
<xs:element name="product">
<xs:complexType>
<xs:sequence><xs:element name="name" type="xs:string"/></xs:sequence>
<xs:attribute name="id" type="xs:integer" use="required"/>
</xs:complexType>
</xs:element>
</xs:schema>Fix the earliest contract mismatch
One unexpected child can shift the validator's view of the whole remaining sequence. Reduce the instance to its root and the smallest failing branch, fix the first message, and validate again before acting on the rest.
Keep a minimal valid instance beside each schema version. It proves that the schema set resolves and gives later failures a known-good comparison point.
Prove the fix
- Run the XSD validatorPaste the intended schema and the smallest failing instance together.
- Match the first schema errorUse the exact validation message to reach the relevant content rule.
- Inspect XSD datatypesCheck lexical rules when structure is correct but a value still fails.
Related guides
- XSD include vs importUse xs:include for one namespace and xs:import across namespaces, then make schema resolution deterministic in local and deployed environments.
- XSD minOccurs vs nillableThe difference between an absent element and a present element with xsi:nil, with schemas and instance documents for each case.
- XSD sequence vs choiceWhen child order is fixed, when alternatives are exclusive, and how minOccurs and maxOccurs change the meaning of an XSD compositor.
Get started
Bring order to the XML your team can't afford to ignore.
Create a free account and get a private workspace to search, validate, diff, and monitor your XML feeds, sitemaps, schemas, and vendor integrations.