XML Schema (XSD): Define XML Structure and Datatype Constraints
XML Schema Definition Language (XSD) is a W3C language for describing the elements, attributes, content structure, and datatype constraints allowed in XML documents. An XML parser checks well-formed syntax; a schema validator additionally checks conformance to a selected XSD contract. This article uses W3C XSD 1.1, so verify the version supported by your actual validator.
XML Schema Definition Language (XSD) is a W3C language for describing the elements, attributes, content structure, and datatype constraints allowed in XML documents. An XML parser checks well-formed syntax; a schema validator additionally checks conformance to a selected XSD contract. This article uses W3C XSD 1.1, so verify the version supported by your actual validator.
What does XSD describe?
XSD expresses namespaces, element and attribute declarations, simple and complex types, value constraints, and rules for child-element combinations. Complex types describe structure such as child elements and attributes; simple types constrain values such as strings, integers, or dates. Validation can find missing or incorrectly typed data, but it does not prove business meaning, authorization, or external system state.
A small schema example
This example says an order in the urn:example:orders namespace has a required orderId attribute and at least one item; each item has a required quantity that is a positive integer.
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="urn:example:orders"
xmlns:ord="urn:example:orders"
elementFormDefault="qualified">
<xs:element name="order">
<xs:complexType>
<xs:sequence>
<xs:element name="item" minOccurs="1" maxOccurs="unbounded">
<xs:complexType>
<xs:attribute name="quantity" type="xs:positiveInteger" use="required"/>
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="orderId" type="xs:token" use="required"/>
</xs:complexType>
</xs:element>
</xs:schema>targetNamespace assigns the namespace for global declarations. elementFormDefault="qualified" requires local elements to use that namespace too. The prefix xs is not special by itself; what matters is that it maps to the XML Schema namespace URI. An instance document may use another prefix bound to the same URI.
Read structure and value constraints
xs:sequence specifies child-element order; xs:choice can allow one of a set of alternatives. minOccurs and maxOccurs constrain occurrence counts. xs:attribute declares an attribute, and use="required" makes it mandatory. Built-in types such as xs:positiveInteger, or facets under xs:simpleType and xs:restriction, can constrain value domains, lengths, or enumerations. Datatype details such as date and time lexical spaces follow XSD Datatypes; do not assume they exactly match a programming language's types.
Validation workflow
Check that the XML is well-formed first: tags, quotes, and document structure must satisfy XML syntax.
Choose the XSD version and schema files, and inspect the namespaces and imported or included schemas.
Run a trusted parser and validator with an explicit schema. Restrict external resource access; do not automatically follow arbitrary `schemaLocation` values from input documents.
For failures, check namespace URIs, element order, required attributes, occurrence counts, types, and facets.
After schema validation passes, apply business-rule and authorization checks separately.
Well-formed XML can still be invalid against a schema because it violates element, ordering, or type constraints. Passing validation means the document meets that structural contract; it does not guarantee that the data is a valid business order.
XSD versions and processor compatibility
W3C publishes separate XSD 1.0 and 1.1 specifications. XSD 1.1 retains core 1.0 features and adds improvements, but not every library or platform implements 1.1. Check both the features used by your schema and the validator version, then validate representative documents with the production tool.
Sources
[W3C XSD 1.1 Part 1: Structures](https://www.w3.org/TR/xmlschema11-1/)
[W3C XSD 1.1 Part 2: Datatypes](https://www.w3.org/TR/xmlschema/)