XSD Example
Practical examples of XML Schema Definition
📋 What is XSD?
XSD (XML Schema Definition) defines the structure, content, and data types of XML documents. It validates XML files to ensure they follow specific rules and formats for data integrity.
<!-- Simple XSD Example -->
<xs:element name="message" type="xs:string"/>
XSD Components
Elements
Define XML elements and their types
<xs:element name="title"
type="xs:string"/>
Attributes
Define element attributes
<xs:attribute name="id"
type="xs:integer"/>
Simple Types
Basic data types for content
<xs:simpleType name="age">
<xs:restriction base="xs:integer"/>
</xs:simpleType>
Complex Types
Define elements with child elements
<xs:complexType name="person">
<xs:sequence>...</xs:sequence>
</xs:complexType>
🔹 Simple XSD Example
A basic schema defining a note with simple elements. This example shows how to create a schema file and validate XML documents against it.
<!-- note.xsd -->
<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>
<!-- Valid XML -->
<note>
<to>John</to>
<from>Jane</from>
<heading>Reminder</heading>
<body>Don't forget the meeting!</body>
</note>
Result:
✅ XML validates against the schema
✅ All required elements are present
🔹 XSD with Attributes
Define elements with attributes to add metadata. Attributes can be required or optional and have specific data types for validation.
<!-- Schema with attributes -->
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="book">
<xs:complexType>
<xs:sequence>
<xs:element name="title" type="xs:string"/>
<xs:element name="author" type="xs:string"/>
</xs:sequence>
<xs:attribute name="isbn" type="xs:string" use="required"/>
<xs:attribute name="year" type="xs:integer"/>
</xs:complexType>
</xs:element>
</xs:schema>
<!-- Valid XML -->
<book isbn="978-0-123456-78-9" year="2024">
<title>Learning XML</title>
<author>John Doe</author>
</book>
Result:
✅ isbn attribute is required
✅ year attribute is optional
🔹 XSD with Restrictions
Apply constraints to element values using restrictions. This ensures data meets specific criteria like minimum/maximum values, patterns, or length requirements.
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<!-- Age with min/max restriction -->
<xs:element name="age">
<xs:simpleType>
<xs:restriction base="xs:integer">
<xs:minInclusive value="0"/>
<xs:maxInclusive value="120"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<!-- Email with pattern restriction -->
<xs:element name="email">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="[^@]+@[^@]+\.[^@]+"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
</xs:schema>
<!-- Valid XML -->
<age>25</age>
<email>[email protected]</email>
Common Restrictions:
- minInclusive/maxInclusive: Number ranges
- pattern: Regular expression matching
- length: Exact string length
- enumeration: Allowed values list
🔹 Complete Example: Product Catalog
A comprehensive schema for a product catalog with multiple elements, attributes, and data types:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="catalog">
<xs:complexType>
<xs:sequence>
<xs:element name="product" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="name" type="xs:string"/>
<xs:element name="price" type="xs:decimal"/>
<xs:element name="description" type="xs:string"/>
<xs:element name="inStock" type="xs:boolean"/>
</xs:sequence>
<xs:attribute name="id" type="xs:integer" use="required"/>
<xs:attribute name="category" type="xs:string"/>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
<!-- Valid XML -->
<catalog>
<product id="101" category="Electronics">
<name>Laptop</name>
<price>899.99</price>
<description>High-performance laptop</description>
<inStock>true</inStock>
</product>
<product id="102" category="Books">
<name>XML Guide</name>
<price>29.99</price>
<description>Complete XML tutorial</description>
<inStock>true</inStock>
</product>
</catalog>
🔹 Linking XSD to XML
Reference your schema file in XML documents:
<?xml version="1.0"?>
<note xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="note.xsd">
<to>John</to>
<from>Jane</from>
<heading>Reminder</heading>
<body>Meeting at 3 PM</body>
</note>