SyntaxStudy
Sign Up
XML XML Schema (XSD) Basics
XML Beginner 1 min read

XML Schema (XSD) Basics

XML Schema Definition (XSD) is a W3C Recommendation that provides a more powerful schema language for XML than DTDs. An XSD schema is itself an XML document, which means it can be parsed, validated, and transformed using the same tools as any XML document. XSD introduced a rich type system including primitive types (string, integer, decimal, boolean, date, dateTime, and many others) and complex types that define element structures. An XSD schema typically begins with a root element that declares the XML Schema namespace (xmlns:xs="http://www.w3.org/2001/XMLSchema") and optionally a target namespace for the vocabulary it defines. Element declarations use and attribute declarations use . Complex types that define element content (child elements and/or attributes) use , while simple types that constrain text content or attribute values use . XSD supports type derivation through restriction (narrowing a base type by adding facets) and extension (adding child elements or attributes to an existing type). This inheritance mechanism allows schema authors to build libraries of reusable named types. A schema can be modularized using (to bring in another schema with the same target namespace) and (to reference types from a different namespace).
Example
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    targetNamespace="http://example.com/books"
    xmlns:bk="http://example.com/books"
    elementFormDefault="qualified">

    <!-- Top-level element declaration -->
    <xs:element name="library" type="bk:LibraryType"/>

    <!-- Complex type: library contains 1..∞ book elements -->
    <xs:complexType name="LibraryType">
        <xs:sequence>
            <xs:element name="book" type="bk:BookType"
                        minOccurs="1" maxOccurs="unbounded"/>
        </xs:sequence>
    </xs:complexType>

    <!-- Complex type with attributes and child elements -->
    <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="year"   type="xs:gYear"/>
            <xs:element name="price"  type="bk:PriceType"
                        minOccurs="0"/>
        </xs:sequence>
        <xs:attribute name="id"       type="xs:ID"      use="required"/>
        <xs:attribute name="category" type="bk:CategoryEnum" use="required"/>
    </xs:complexType>

    <!-- Simple type: restriction on string -->
    <xs:simpleType name="CategoryEnum">
        <xs:restriction base="xs:string">
            <xs:enumeration value="fiction"/>
            <xs:enumeration value="non-fiction"/>
            <xs:enumeration value="reference"/>
        </xs:restriction>
    </xs:simpleType>

    <!-- Simple type for price: decimal ≥ 0 -->
    <xs:complexType name="PriceType">
        <xs:simpleContent>
            <xs:extension base="xs:decimal">
                <xs:attribute name="currency" type="xs:string" use="required"/>
            </xs:extension>
        </xs:simpleContent>
    </xs:complexType>

</xs:schema>