XML Schema

XML Schema (XSD) is a more powerful, XML-based alternative to DTD that adds rich data types, namespace awareness, and precise constraints for validating document structure and content.

Why XSD Exists

DTD validation only checks structural shape and treats all text content the same way. XML Schema Definition (XSD) goes further: it validates that a value is genuinely an integer, a date, a decimal within a range, or a string matching a specific pattern. Because XSD is itself written in XML, it can also be edited, generated, and transformed with standard XML tooling.

XSD is also namespace-aware, meaning a schema can precisely describe documents that mix elements from multiple vocabularies — something DTD cannot express natively.

A Basic XSD Document

Schema for a simple note element

<!DOCTYPE html>
<html>
<body>

<pre>
&lt;?xml version="1.0"?&gt;
&lt;xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"&gt;

  &lt;xs:element name="note"&gt;
    &lt;xs:complexType&gt;
      &lt;xs:sequence&gt;
        &lt;xs:element name="to" type="xs:string"/&gt;
        &lt;xs:element name="from" type="xs:string"/&gt;
        &lt;xs:element name="heading" type="xs:string"/&gt;
        &lt;xs:element name="body" type="xs:string"/&gt;
      &lt;/xs:sequence&gt;
    &lt;/xs:complexType&gt;
  &lt;/xs:element&gt;

&lt;/xs:schema&gt;
</pre>

</body>
</html>

Simple Types and Restrictions

Simple types describe element or attribute content that contains no child elements or attributes. XSD ships with many built-in types — xs:string, xs:integer, xs:decimal, xs:date, xs:boolean — and lets you derive your own restricted types using facets like minInclusive, maxLength, and pattern.

Restricting an element to a bounded integer

<!DOCTYPE html>
<html>
<body>

<pre>
&lt;xs:element name="age"&gt;
  &lt;xs:simpleType&gt;
    &lt;xs:restriction base="xs:integer"&gt;
      &lt;xs:minInclusive value="0"/&gt;
      &lt;xs:maxInclusive value="120"/&gt;
    &lt;/xs:restriction&gt;
  &lt;/xs:simpleType&gt;
&lt;/xs:element&gt;
</pre>

</body>
</html>

Complex Types with Attributes

Complex types describe elements that contain child elements, attributes, or both. Attributes are declared with xs:attribute, and can specify a use of required or optional.

A complex type with a required attribute

<!DOCTYPE html>
<html>
<body>

<pre>
&lt;xs:element name="book"&gt;
  &lt;xs:complexType&gt;
    &lt;xs:sequence&gt;
      &lt;xs:element name="title" type="xs:string"/&gt;
      &lt;xs:element name="price" type="xs:decimal"/&gt;
    &lt;/xs:sequence&gt;
    &lt;xs:attribute name="isbn" type="xs:string" use="required"/&gt;
  &lt;/xs:complexType&gt;
&lt;/xs:element&gt;
</pre>

</body>
</html>

Controlling Occurrence

minOccurs and maxOccurs control how many times an element may repeat, replacing the terser but less flexible ?, *, and + symbols used in DTD. The value "unbounded" allows unlimited repetitions.

  • minOccurs="0" makes an element optional, equivalent to DTD's ? symbol
  • maxOccurs="unbounded" allows unlimited repeats, equivalent to DTD's * or +
  • xs:choice lets you require exactly one of several alternative elements
  • xs:all allows child elements in any order, which DTD sequences cannot express
  • Named complex types can be reused across multiple element declarations
CapabilityDTDXSD
Written inSpecial DTD syntaxXML itself
Data typingText only (#PCDATA)Rich types: integer, date, decimal, etc.
Namespace supportNoneFull namespace awareness
Custom type restrictionsNot supportedFacets like pattern, minInclusive, length
ReusabilityLimited to entities and external subsetsNamed types, imports, and includes
Note: Every XML Schema document uses the http://www.w3.org/2001/XMLSchema namespace, conventionally aliased to xs: or xsd:, which is why schema elements are prefixed that way throughout examples.
Note: XSD's verbosity is a real tradeoff — a schema for a moderately complex document can run to hundreds of lines, considerably more than the equivalent DTD, in exchange for far stronger validation guarantees.

Exercise: XML Schema

What is a key advantage of XML Schema (XSD) over DTD?