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:

FeatureDTDXSD
SyntaxOwn special syntaxWritten in XML
Data typesNone (only text)Rich built-in types (integer, date, boolean, etc.)
Namespace supportLimitedFull namespace support
Custom typesNot possibleCan define custom types with restrictions
ExtensibilityNot extensibleSchemas 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 TypeDescriptionExample Value
xs:stringTextHello World
xs:integerWhole number42
xs:decimalDecimal number19.99
xs:booleanTrue or falsetrue
xs:dateCalendar date2024-03-15
xs:timeTime of day14:30:00
xs:dateTimeCombined date and time2024-03-15T14:30:00
xs:anyURIA URI or URLhttps://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

CompositorMeaning
<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.
  • minOccurs and maxOccurs control 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.

Leave a Comment

Your email address will not be published. Required fields are marked *