XML Schema

Define XML structure with powerful data types and constraints

📐 What is XML Schema?

XML Schema (XSD) is a more powerful alternative to DTD for defining XML structure. It supports data types, namespaces, and complex validation rules, making it ideal for modern XML applications.


<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="note" type="xs:string"/>
</xs:schema>
                                    

Key Schema Concepts

🔤

Data Types

Built-in type validation

type="xs:integer"
🏗️

Complex Types

Define element structures

<xs:complexType>

Restrictions

Constrain values

<xs:minInclusive>
🌐

Namespaces

Avoid naming conflicts

xmlns:xs="..."

🔹 Basic XML Schema

XML Schema documents are written in XML and define the structure of other XML documents. They specify elements, their data types, and validation rules. Schemas are more powerful than DTDs and support modern XML features like namespaces.

Schema File (note.xsd):

<?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>

XML Document:

<?xml version="1.0"?>
<note xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:noNamespaceSchemaLocation="note.xsd">
    <to>Alice</to>
    <from>Bob</from>
    <heading>Reminder</heading>
    <body>Don't forget the meeting!</body>
</note>

🔹 Simple Types

Simple types define elements that contain only text with no child elements or attributes. XML Schema provides many built-in types like string, integer, date, and boolean. You can also create custom simple types with restrictions.

<!-- Built-in simple types -->
<xs:element name="name" type="xs:string"/>
<xs:element name="age" type="xs:integer"/>
<xs:element name="price" type="xs:decimal"/>
<xs:element name="birthdate" type="xs:date"/>
<xs:element name="active" type="xs:boolean"/>

<!-- Custom simple type with restriction -->
<xs:simpleType name="ageType">
    <xs:restriction base="xs:integer">
        <xs:minInclusive value="0"/>
        <xs:maxInclusive value="120"/>
    </xs:restriction>
</xs:simpleType>

<xs:element name="age" type="ageType"/>

Common Built-in Types:

xs:string - Text data

xs:integer - Whole numbers

xs:decimal - Decimal numbers

xs:date - Date (YYYY-MM-DD)

xs:boolean - true/false

🔹 Complex Types

Complex types define elements that contain child elements or attributes. They allow you to create structured data with nested elements. Use sequence for ordered elements, choice for alternatives, and all for unordered elements.

<!-- Complex type with sequence -->
<xs:complexType name="bookType">
    <xs:sequence>
        <xs:element name="title" type="xs:string"/>
        <xs:element name="author" type="xs:string"/>
        <xs:element name="price" type="xs:decimal"/>
    </xs:sequence>
    <xs:attribute name="id" type="xs:string" use="required"/>
</xs:complexType>

<xs:element name="book" type="bookType"/>

<!-- Complex type with choice -->
<xs:complexType name="contactType">
    <xs:choice>
        <xs:element name="email" type="xs:string"/>
        <xs:element name="phone" type="xs:string"/>
    </xs:choice>
</xs:complexType>

🔹 Restrictions and Facets

Restrictions allow you to constrain values using facets. Facets define specific rules like minimum/maximum values, string length, patterns, and enumerated values. This provides powerful validation beyond simple data types.

<!-- String length restriction -->
<xs:simpleType name="usernameType">
    <xs:restriction base="xs:string">
        <xs:minLength value="3"/>
        <xs:maxLength value="20"/>
    </xs:restriction>
</xs:simpleType>

<!-- Pattern restriction (regex) -->
<xs:simpleType name="emailType">
    <xs:restriction base="xs:string">
        <xs:pattern value="[^@]+@[^@]+\.[^@]+"/>
    </xs:restriction>
</xs:simpleType>

<!-- Enumeration -->
<xs:simpleType name="categoryType">
    <xs:restriction base="xs:string">
        <xs:enumeration value="fiction"/>
        <xs:enumeration value="non-fiction"/>
        <xs:enumeration value="reference"/>
    </xs:restriction>
</xs:simpleType>

<!-- Numeric range -->
<xs:simpleType name="percentType">
    <xs:restriction base="xs:integer">
        <xs:minInclusive value="0"/>
        <xs:maxInclusive value="100"/>
    </xs:restriction>
</xs:simpleType>

🔹 Occurrence Indicators

Occurrence indicators control how many times an element can appear. Use minOccurs and maxOccurs to specify exact counts, ranges, or unlimited occurrences. This provides flexible validation for repeating elements.

<xs:element name="library">
    <xs:complexType>
        <xs:sequence>
            <!-- Exactly one -->
            <xs:element name="name" type="xs:string"/>
            
            <!-- Optional (0 or 1) -->
            <xs:element name="description" type="xs:string" 
                        minOccurs="0" maxOccurs="1"/>
            
            <!-- One or more -->
            <xs:element name="book" type="bookType" 
                        minOccurs="1" maxOccurs="unbounded"/>
            
            <!-- Zero or more -->
            <xs:element name="review" type="xs:string" 
                        minOccurs="0" maxOccurs="unbounded"/>
        </xs:sequence>
    </xs:complexType>
</xs:element>

🔹 Complete Schema Example

This comprehensive example demonstrates a complete XML Schema for a bookstore catalog. It includes simple types, complex types, attributes, restrictions, and occurrence indicators working together to create a robust validation schema.

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

    <!-- Simple type definitions -->
    <xs:simpleType name="isbnType">
        <xs:restriction base="xs:string">
            <xs:pattern value="[0-9]{3}-[0-9]{10}"/>
        </xs:restriction>
    </xs:simpleType>

    <xs:simpleType name="categoryType">
        <xs:restriction base="xs:string">
            <xs:enumeration value="fiction"/>
            <xs:enumeration value="non-fiction"/>
            <xs:enumeration value="reference"/>
        </xs:restriction>
    </xs:simpleType>

    <!-- Complex type for book -->
    <xs:complexType name="bookType">
        <xs:sequence>
            <xs:element name="title" type="xs:string"/>
            <xs:element name="author" type="xs:string" 
                        minOccurs="1" maxOccurs="unbounded"/>
            <xs:element name="isbn" type="isbnType"/>
            <xs:element name="price" type="xs:decimal"/>
            <xs:element name="description" type="xs:string" 
                        minOccurs="0"/>
        </xs:sequence>
        <xs:attribute name="id" type="xs:ID" use="required"/>
        <xs:attribute name="category" type="categoryType" 
                      default="fiction"/>
    </xs:complexType>

    <!-- Root element -->
    <xs:element name="bookstore">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="book" type="bookType" 
                            minOccurs="1" maxOccurs="unbounded"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>

</xs:schema>

🧠 Test Your Knowledge

What is the main advantage of XML Schema over DTD?