XML Schema Definition (XSD)

XML Schema Definition (XSD) is a powerful language for defining the structure and data types of XML documents. Unlike Document Type Definition (DTD), XSD is written in XML, making it more versatile and extensible. XSD allows you to define the elements, attributes, and data types of an XML document in a precise and structured manner.

What is XSD?

XML Schema Definition (XSD) is an XML-based language used to describe the structure and validate the content of XML documents. XSD provides a way to define the legal elements and attributes in an XML document and their data types, constraints, and relationships.

Why Use XSD?

Using XSD has several advantages:

  • Validation: Ensures that the XML document adheres to a predefined structure and rules.
  • Data Integrity: Enforces data types and constraints, maintaining consistency and accuracy.
  • Extensibility: Being XML-based, XSD is more flexible and can be extended as needed.
  • Interoperability: Facilitates data exchange between different systems by standardizing XML structures.

Basic Structure of XSD

An XSD file typically contains definitions for:

  • Elements: Define the structure and content of XML elements.
  • Attributes: Define properties associated with elements.
  • Data Types: Define the types of data that can be used within elements and attributes.
  • Complex Types: Define structures that contain multiple elements and attributes.
				
					<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    
</xs:schema>

				
			

Defining Simple Elements

Simple elements contain only text and no child elements or attributes.

Syntax

				
					<xs:element name="element-name" type="xs:data-type"/>

				
			

Example

				
					<xs:element name="firstName" type="xs:string"/>

				
			

XML Document

				
					<firstName>John</firstName>

				
			

Defining Complex Elements

Complex elements can contain other elements and attributes.

Syntax

				
					<xs:element name="element-name">
    <xs:complexType>
        
    </xs:complexType>
</xs:element>

				
			

Example

				
					<xs:element name="person">
    <xs:complexType>
        <xs:sequence>
            <xs:element name="firstName" type="xs:string"/>
            <xs:element name="lastName" type="xs:string"/>
        </xs:sequence>
    </xs:complexType>
</xs:element>

				
			

XML Document

				
					<person>
    <firstName>John</firstName>
    <lastName>Doe</lastName>
</person>

				
			

Defining Attributes

Attributes provide additional information about elements.

Syntax

				
					<xs:attribute name="attribute-name" type="xs:data-type"/>

				
			

Example

				
					<xs:element name="person">
    <xs:complexType>
        <xs:sequence>
            <xs:element name="firstName" type="xs:string"/>
            <xs:element name="lastName" type="xs:string"/>
        </xs:sequence>
        <xs:attribute name="id" type="xs:integer"/>
    </xs:complexType>
</xs:element>

				
			

XML Document

				
					<person id="1">
    <firstName>John</firstName>
    <lastName>Doe</lastName>
</person>

				
			

Data Types in XSD

XSD supports a wide range of data types, including:

  • String: xs:string
  • Numeric: xs:integer, xs:decimal, xs:float, xs:double
  • Date/Time: xs:date, xs:time, xs:dateTime
  • Boolean: xs:boolean

Example

				
					<xs:element name="age" type="xs:integer"/>
<xs:element name="birthDate" type="xs:date"/>
<xs:element name="isMarried" type="xs:boolean"/>

				
			

XML Document

				
					<age>30</age>
<birthDate>1990-01-01</birthDate>
<isMarried>false</isMarried>

				
			

Defining Complex Types

Complex types allow you to create reusable structures that contain multiple elements and attributes.

Syntax

				
					<xs:complexType name="complex-type-name">
    <xs:sequence>
        
    </xs:sequence>
    
</xs:complexType>

				
			

Example

				
					<xs:complexType name="address">
    <xs:sequence>
        <xs:element name="street" type="xs:string"/>
        <xs:element name="city" type="xs:string"/>
        <xs:element name="postalCode" type="xs:string"/>
    </xs:sequence>
</xs:complexType>

				
			

XML Document

				
					<address>
    <street>123 Main St</street>
    <city>Anytown</city>
    <postalCode>12345</postalCode>
</address>

				
			

Using XSD in XML

To use an XSD in an XML document, you reference it using the 'xsi:schemaLocation' attribute in the XML document’s root element.

Syntax

				
					<root-element xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="namespace schema-file.xsd">
    
</root-element>

				
			

Example

				
					<person xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.example.com/person person.xsd">
    <firstName>John</firstName>
    <lastName>Doe</lastName>
</person>

				
			

Namespaces in XSD

Namespaces are used to avoid naming conflicts in XML documents. They are defined using the xmlns attribute.

Syntax

				
					<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="namespace" xmlns="namespace" elementFormDefault="qualified">
    
</xs:schema>

				
			

Example

				
					<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.example.com/person" xmlns="http://www.example.com/person" elementFormDefault="qualified">
    <xs:element name="person">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="firstName" type="xs:string"/>
                <xs:element name="lastName" type="xs:string"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>

				
			

XML Document

				
					<person xmlns="http://www.example.com/person">
    <firstName>John</firstName>
    <lastName>Doe</lastName>
</person>

				
			

Advanced Concepts in XSD

Key and KeyRef

Key and KeyRef are used to define and reference unique keys within an XML document.

Syntax

				
					<xs:key name="keyName">
    <xs:selector xpath="xpathExpression"/>
    <xs:field xpath="xpathExpression"/>
</xs:key>

<xs:keyref name="keyRefName" refer="keyName">
    <xs:selector xpath="xpathExpression"/>
    <xs:field xpath="xpathExpression"/>
</xs:keyref>

				
			

Example

				
					<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="library">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="book" maxOccurs="unbounded">
                    <xs:complexType>
                        <xs:sequence>
                            <xs:element name="title" type="xs:string"/>
                            <xs:element name="author" type="xs:string"/>
                        </xs:sequence>
                        <xs:attribute name="id" type="xs:ID" use="required"/>
                    </xs:complexType>
                </xs:element>
                <xs:element name="borrowedBooks" maxOccurs="unbounded">
                    <xs:complexType>
                        <xs:sequence>
                            <xs:element name="bookID" type="xs:IDREF"/>
                        </xs:sequence>
                    </xs:complexType>
                </xs:element>
            </xs:sequence>
        </xs:complexType>
        <xs:key name="bookKey">
            <xs:selector xpath="book"/>
            <xs:field xpath="@id"/>
        </xs:key>
        <xs:keyref name="borrowedBookRef" refer="bookKey">
            <xs:selector xpath="borrowedBooks"/>
            <xs:field xpath="bookID"/>
        </xs:keyref>
    </xs:element>
</xs:schema>

				
			

Advanced Constraints with Facets

Here we define a custom data type for a phone number that must follow a specific pattern and length.

				
					<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    
    <xs:simpleType name="PhoneNumber">
        <xs:restriction base="xs:string">
            <xs:pattern value="\d{3}-\d{3}-\d{4}"/>
        </xs:restriction>
    </xs:simpleType>

    
    <xs:element name="contact">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="name" type="xs:string"/>
                <xs:element name="phone" type="PhoneNumber"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>

				
			

In this schema, the 'PhoneNumber' type enforces that phone numbers must follow the pattern 'XXX-XXX-XXXX' where 'X' is a digit.

Using Complex Types with Mixed Content

Here we define a complex type that allows mixed content, combining both text and child elements.

				
					<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    
    <xs:complexType name="richText">
        <xs:complexContent mixed="true">
            <xs:sequence>
                <xs:element name="bold" type="xs:string" minOccurs="0" maxOccurs="unbounded"/>
                <xs:element name="italic" type="xs:string" minOccurs="0" maxOccurs="unbounded"/>
            </xs:sequence>
        </xs:complexContent>
    </xs:complexType>

    
    <xs:element name="document">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="content" type="richText"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>

				
			

In this schema, the richText type allows mixed content, meaning text can be interspersed with bold and italic elements. This is useful for documents with complex formatting.

Using Substitution Groups

Substitution groups allow one element to be substituted by another in an XML document, providing a way to create flexible and extensible schemas.

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

    
    <xs:element name="circle" type="xs:string" substitutionGroup="shape"/>
    <xs:element name="square" type="xs:string" substitutionGroup="shape"/>
    
    
    <xs:complexType name="shapes">
        <xs:sequence>
            <xs:element ref="shape" maxOccurs="unbounded"/>
        </xs:sequence>
    </xs:complexType>
    
    
    <xs:element name="drawing" type="shapes"/>
</xs:schema>

				
			

In this schema, the shape element is an abstract head of a substitution group, and circle and square elements can substitute it. The drawing element can contain any number of shapes.

Using Annotations for Documentation

Annotations provide a way to include documentation within your schema, making it easier to understand and maintain.

				
					<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    
    <xs:annotation>
        <xs:documentation>
            This schema defines a simple library system with books and authors.
        </xs:documentation>
    </xs:annotation>

    
    <xs:element name="book">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="title" type="xs:string">
                    <xs:annotation>
                        <xs:documentation>The title of the book.</xs:documentation>
                    </xs:annotation>
                </xs:element>
                <xs:element name="author" type="xs:string">
                    <xs:annotation>
                        <xs:documentation>The author of the book.</xs:documentation>
                    </xs:annotation>
                </xs:element>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>

				
			

In this schema, annotations provide documentation for the schema and individual elements, helping users understand the purpose and structure of the elements.

Examples

Example 1: Simple XSD Schema

				
					<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:attribute name="date" type="xs:date" use="required"/>
        </xs:complexType>
    </xs:element>
</xs:schema>

				
			

Example 2: Using Custom Data Types

				
					<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:simpleType name="gender">
        <xs:restriction base="xs:string">
            <xs:enumeration value="Male"/>
            <xs:enumeration value="Female"/>
        </xs:restriction>
    </xs:simpleType>
    
    <xs:element name="person">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="name" type="xs:string"/>
                <xs:element name="gender" type="gender"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>

				
			

Example 3: Namespaces in XSD

				
					<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.example.com/schema" xmlns="http://www.example.com/schema">
    <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:attribute name="date" type="xs:date" use="required"/>
        </xs:complexType>
    </xs:element>
</xs:schema>

				
			

XML Schema Definition (XSD) is a powerful and flexible tool for defining the structure and data types of XML documents. It offers a wide range of features for specifying complex data structures, custom data types, and constraints. By mastering XSD, you can ensure the integrity, consistency, and interoperability of your XML data, making it easier to manage and share across different systems and applications. Happy Coding! ❤️

Table of Contents