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>
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="note">
<xs:complexType>
<xs:sequence>
<xs:element name="to" type="xs:string"/>
<xs:element name="from" type="xs:string"/>
<xs:element name="heading" type="xs:string"/>
<xs:element name="body" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
</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>
<xs:element name="age">
<xs:simpleType>
<xs:restriction base="xs:integer">
<xs:minInclusive value="0"/>
<xs:maxInclusive value="120"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
</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>
<xs:element name="book">
<xs:complexType>
<xs:sequence>
<xs:element name="title" type="xs:string"/>
<xs:element name="price" type="xs:decimal"/>
</xs:sequence>
<xs:attribute name="isbn" type="xs:string" use="required"/>
</xs:complexType>
</xs:element>
</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
Exercise: XML Schema
What is a key advantage of XML Schema (XSD) over DTD?