TExcelTAD
Home › XML › XML Schema

XML Schema (XSD)

XML Schema, often called XSD (XML Schema Definition), is the W3C's modern language for describing and validating XML documents. It does everything a DTD does, and adds data types, namespaces, precise occurrence counts and reusable types — and a schema is itself an XML document.

Note: Browsers can't validate against XSD. The schemas here are well-formed XML you can run through the playground's parser; for real validation use xmllint --schema, an IDE plugin, or a library in your server language.

A first schema

The document to describe:

xml · try it yourself
<?xml version="1.0" encoding="UTF-8"?>
<order id="A-1043" date="2026-05-02">
  <customer>Lena Fischer</customer>
  <item sku="TEA-01" qty="2">
    <name>Sencha green tea</name>
    <price>6.50</price>
  </item>
  <item sku="MUG-07" qty="1">
    <name>Stoneware mug</name>
    <price>14.00</price>
  </item>
</order>

A schema for it:

xml · try it yourself
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">

  <xs:element name="order">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="customer" type="xs:string"/>
        <xs:element name="item" type="ItemType" minOccurs="1" maxOccurs="unbounded"/>
      </xs:sequence>
      <xs:attribute name="id" type="xs:string" use="required"/>
      <xs:attribute name="date" type="xs:date" use="required"/>
    </xs:complexType>
  </xs:element>

  <xs:complexType name="ItemType">
    <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:attribute name="qty" type="xs:positiveInteger" default="1"/>
  </xs:complexType>

</xs:schema>

Now <price>cheap</price>, qty="0" or date="May 2nd" would all fail validation — none of which a DTD could catch.

Simple and complex types

Simple type Complex type
Can contain Text only Child elements and/or attributes
Used for Element text, attribute values Elements with structure
Example xs:decimal, xs:date ItemType above

Built-in data types

Type Example values
xs:string Hello
xs:boolean true, false, 1, 0
xs:integer, xs:positiveInteger, xs:nonNegativeInteger -3, 42
xs:decimal, xs:double 6.50, 1.2E3
xs:date, xs:time, xs:dateTime 2026-05-02, 14:30:00, 2026-05-02T14:30:00Z
xs:duration P1DT2H (1 day 2 hours)
xs:anyURI https://example.com
xs:ID, xs:IDREF as in DTDs

Restrictions (facets)

Derive your own simple types by restricting a built-in one:

xml · try it yourself
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:simpleType name="SkuType">
    <xs:restriction base="xs:string">
      <xs:pattern value="[A-Z]{3}-[0-9]{2}"/>
    </xs:restriction>
  </xs:simpleType>

  <xs:simpleType name="RatingType">
    <xs:restriction base="xs:integer">
      <xs:minInclusive value="1"/>
      <xs:maxInclusive value="5"/>
    </xs:restriction>
  </xs:simpleType>

  <xs:simpleType name="SizeType">
    <xs:restriction base="xs:string">
      <xs:enumeration value="S"/>
      <xs:enumeration value="M"/>
      <xs:enumeration value="L"/>
    </xs:restriction>
  </xs:simpleType>
</xs:schema>
Facet Restricts
minInclusive / maxInclusive (and …Exclusive) Numeric or date range
enumeration A fixed list of values
pattern A regular expression
length, minLength, maxLength String length
totalDigits, fractionDigits Number of digits

Compositors and occurrences

Compositor Children must appear…
xs:sequence in the given order
xs:choice exactly one of the alternatives
xs:all in any order, each at most once

minOccurs and maxOccurs (default 1 each; maxOccurs="unbounded" for no limit) work on any element or compositor — far more precise than the DTD's ?, * and +.

Linking a document to its schema

xml
<order xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:noNamespaceSchemaLocation="order.xsd"
       id="A-1043" date="2026-05-02">

For documents in a namespace, use targetNamespace in the schema and xsi:schemaLocation="namespace-uri schema-file" in the document.

DTD vs XSD

DTD XML Schema
Syntax Special, non-XML XML
Data types Almost none 40+ built-in, plus your own
Namespaces Not supported Fully supported
Occurrence control ? * + Any minOccurs/maxOccurs
Entities Yes No
Learning curve Small Larger

Other schema languages exist too: RELAX NG (simpler and very expressive) and Schematron (rule-based checks using XPath, great for business rules such as "the total must equal the sum of the items").

Test yourself

Which XSD type would reject the value cheap for a price?

  1. xs:string
  2. xs:decimal
  3. xs:anyType
  4. xs:token

How do you allow an element to repeat without limit?

  1. maxOccurs="*"
  2. maxOccurs="unbounded"
  3. repeat="true"
  4. minOccurs="many"

Which compositor requires children in a fixed order?

  1. xs:all
  2. xs:choice
  3. xs:sequence
  4. xs:group