XML Schema defines a vocabulary for XML. It can declare elements and attributes, build reusable types, constrain values and compose declarations across namespaces. A validator uses those declarations to determine whether an XML instance belongs to the described language.
What an XSD can describe
An XSD can require a root element, order child elements in a sequence, allow alternatives, control repetition, define attributes and constrain text to datatypes or value ranges. It also assigns namespace-aware identity to declarations.
Schema validation adds information beyond XML parsing. The XML Formatter can confirm well-formed syntax, but only an XSD processor can determine whether a parsed document satisfies a schema set.
Anatomy of a schema document
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="https://example.com/catalog"
xmlns:tns="https://example.com/catalog"
elementFormDefault="qualified">
<xs:element name="catalog" type="tns:CatalogType"/>
</xs:schema>The xs prefix refers to the XML Schema vocabulary. targetNamespace identifies the declarations created by the schema. The tns prefix is a convenient binding used when those declarations are referenced by QName.
Elements, attributes and types
An element declaration gives an XML element its name and type. A simple type describes atomic text values, lists or unions. A complex type describes child content and attributes. Types can be built in, named globally or anonymous within a declaration.
<xs:complexType name="ProductType">
<xs:sequence>
<xs:element name="name" type="xs:string"/>
<xs:element name="price" type="xs:decimal"/>
</xs:sequence>
<xs:attribute name="sku" type="xs:string" use="required"/>
</xs:complexType>Use the guide to element references versus named types when deciding whether identity or structure is the reusable concept.
Cardinality and value constraints
minOccurs and maxOccurs control how many times an element particle can occur. Simple-type facets constrain values with ranges, lengths, patterns and enumerations. Restrictions should reflect domain rules and should be tested with boundary examples.
| Constraint | Purpose | Example |
|---|---|---|
minOccurs="0" | Element may be absent | Optional note |
maxOccurs="unbounded" | Repeat without schema limit | Line items |
xs:enumeration | Closed value set | Status code |
xs:pattern | Lexical pattern | Identifier syntax |
Schema documents, includes and imports
A vocabulary can span several schema documents. xs:include combines documents for the same target namespace. xs:import makes declarations from another namespace available. Relative locations, catalogs and resolver policies must work consistently in development, CI and production.
A production validator should load the complete schema set under an explicit resource policy. Avoid depending on uncontrolled remote network retrieval during validation.
Continue the learning path
- Compare global and local XSD design patterns.
- Apply the XML Schema best-practice checklist.
- Learn rule-based validation with Schematron.
- Plan compatible changes with the schema versioning guide.
- Inspect a real XSD with the Pattern Analyzer.
Frequently asked questions
01What is XML Schema?
XML Schema, commonly called XSD, is a W3C schema language for describing XML document structure, element and attribute declarations, datatypes and constraints.
02What is the difference between XML and XSD?
XML is a syntax for documents. XSD is an XML-based language that describes which XML documents belong to a vocabulary and which structural and datatype constraints they must satisfy.
03Can one XML document use multiple XSD files?
Yes. A schema set can contain multiple documents connected with xs:include for the same namespace and xs:import for declarations in other namespaces.
04What is an xs:complexType?
A complex type describes element content, attributes or mixed content. It can be named globally for reuse or declared anonymously inside an element.
05What is an xs:simpleType?
A simple type constrains text or attribute values without child elements. It can restrict a base type with facets, create lists or combine alternatives with union.
06Does XSD validate business rules?
XSD covers many structural and datatype constraints. Contextual or cross-field rules may require XSD 1.1 assertions, Schematron or an application validation layer.
07What is XSD 1.1?
XSD 1.1 extends XML Schema with features including assertions, conditional type alternatives and other improvements. Processor support must be checked because XSD 1.0 remains widespread.
08How is an XML document linked to an XSD?
An instance can use xsi:schemaLocation or xsi:noNamespaceSchemaLocation as hints, but applications may select schemas externally. A hint is not a security or trust policy.
09Is a well-formed XML document automatically valid?
No. Well-formedness checks XML syntax. Validity means the document also satisfies the declarations and constraints of an applied schema or rule set.
10Which XSD design pattern should beginners use?
Venetian Blind is a useful starting point for many vocabularies: a small set of global root elements, reusable named types and local child elements. Requirements should still drive the final pattern.