XML Schema (XSD)
XML Schema Definition — commonly called XSD — is the modern, powerful way to define the structure, content, and data types of an XML document. XSD overcomes the significant limitations of DTD by using XML syntax itself and supporting a rich set of data types. It is now the industry standard for validating XML documents.
Why XSD Over DTD?
DTD served well in the early days of XML, but it had several weaknesses. XSD was designed to address all of them:
| Feature | DTD | XSD |
|---|---|---|
| Syntax | Own special syntax | Written in XML |
| Data types | None (only text) | Rich built-in types (integer, date, boolean, etc.) |
| Namespace support | Limited | Full namespace support |
| Custom types | Not possible | Can define custom types with restrictions |
| Extensibility | Not extensible | Schemas can extend other schemas |
Structure of an XSD File
An XSD file is a standard XML document with the root element <xs:schema>. The xs prefix is conventionally used for the XML Schema namespace.
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<!-- Element and type declarations go here -->
</xs:schema>
Linking an XSD File to an XML File
The XML document references its schema using the xsi:noNamespaceSchemaLocation attribute:
<?xml version="1.0" encoding="UTF-8"?>
<note xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="note.xsd">
<to>Anna</to>
<from>David</from>
<message>See you at 3 PM.</message>
</note>
Declaring Simple Elements
A simple element contains only text — no child elements or attributes. It is declared using <xs:element> with a name and a type.
<xs:element name="city" type="xs:string" />
<xs:element name="population" type="xs:integer" />
<xs:element name="founded" type="xs:date" />
Commonly Used Built-In Data Types
| XSD Type | Description | Example Value |
|---|---|---|
xs:string | Text | Hello World |
xs:integer | Whole number | 42 |
xs:decimal | Decimal number | 19.99 |
xs:boolean | True or false | true |
xs:date | Calendar date | 2024-03-15 |
xs:time | Time of day | 14:30:00 |
xs:dateTime | Combined date and time | 2024-03-15T14:30:00 |
xs:anyURI | A URI or URL | https://example.com |
Declaring Complex Elements
A complex element is one that contains child elements, attributes, or both. It is declared with <xs:complexType>.
Example: Element with Child Elements
<xs:element name="person">
<xs:complexType>
<xs:sequence>
<xs:element name="firstName" type="xs:string" />
<xs:element name="lastName" type="xs:string" />
<xs:element name="age" type="xs:integer" />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:sequence> means the child elements must appear in the exact order listed.
Compositors: Controlling Child Element Order and Choice
| Compositor | Meaning |
|---|---|
<xs:sequence> | Children must appear in the given order |
<xs:all> | Children can appear in any order, each at most once |
<xs:choice> | Only one of the listed children can appear |
Occurrence Constraints
The minOccurs and maxOccurs attributes on an element declaration control how many times it must appear.
<xs:element name="phone" type="xs:string" minOccurs="0" maxOccurs="3" />
This means the <phone> element is optional (minOccurs="0") and can appear at most 3 times. Use maxOccurs="unbounded" to allow unlimited repetitions.
Declaring Attributes in XSD
Attributes are declared using <xs:attribute> inside a <xs:complexType>.
<xs:element name="product">
<xs:complexType>
<xs:sequence>
<xs:element name="name" type="xs:string" />
<xs:element name="price" type="xs:decimal" />
</xs:sequence>
<xs:attribute name="id" type="xs:string" use="required" />
<xs:attribute name="currency" type="xs:string" default="USD" />
</xs:complexType>
</xs:element>
The use="required" means the attribute must always be present. If use is omitted, the attribute is optional.
Simple Type Restrictions
XSD allows creating custom data types by restricting an existing type. This is done using <xs:simpleType> and <xs:restriction>.
Example: Restricting a String to Specific Values (Enumeration)
<xs:simpleType name="statusType">
<xs:restriction base="xs:string">
<xs:enumeration value="active" />
<xs:enumeration value="inactive" />
<xs:enumeration value="pending" />
</xs:restriction>
</xs:simpleType>
Example: Restricting a Number to a Range
<xs:simpleType name="ageRange">
<xs:restriction base="xs:integer">
<xs:minInclusive value="0" />
<xs:maxInclusive value="120" />
</xs:restriction>
</xs:simpleType>
Example: Restricting a String Length
<xs:simpleType name="shortCode">
<xs:restriction base="xs:string">
<xs:minLength value="2" />
<xs:maxLength value="5" />
</xs:restriction>
</xs:simpleType>
Complete XSD and XML Example
File: employee.xsd
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="employees">
<xs:complexType>
<xs:sequence>
<xs:element name="employee" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="name" type="xs:string" />
<xs:element name="department" type="xs:string" />
<xs:element name="salary" type="xs:decimal" />
<xs:element name="startDate" type="xs:date" />
</xs:sequence>
<xs:attribute name="id" type="xs:string" use="required" />
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
File: employees.xml
<?xml version="1.0" encoding="UTF-8"?>
<employees xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="employee.xsd">
<employee id="E001">
<name>Nina Patel</name>
<department>Finance</department>
<salary>68000.00</salary>
<startDate>2021-06-01</startDate>
</employee>
<employee id="E002">
<name>Tom Walsh</name>
<department>Engineering</department>
<salary>84000.00</salary>
<startDate>2019-09-15</startDate>
</employee>
</employees>
Key Points
- XSD is the modern replacement for DTD, written in XML syntax.
- XSD supports rich built-in data types like string, integer, date, and boolean.
- Simple elements contain only text; complex elements contain child elements or attributes.
<xs:sequence>,<xs:all>, and<xs:choice>control how child elements are ordered or selected.minOccursandmaxOccurscontrol how many times an element can appear.- Custom data types can be created using
<xs:simpleType>with restrictions. - XSD fully supports XML namespaces, making it suitable for large and complex XML systems.
