Summary of XML Schema Definition (XSD) Guide
XML Schema Definition (XSD) Guide for Students
Introduction
XML Schema Definition (XSD) is a formal language for describing the structure and data types of XML documents. It enables precise XML validation — not just syntactic (well-formedness), but also content-based (types, number of elements, order, attribute values, etc.). This material summarizes the fundamental concepts of XSD, design patterns, and practical examples.
Definition: An XML Schema (XSD) is an XML document (a file with the .xsd extension) that describes the allowed structure, data types, and constraints for XML instances.
Basic Concepts
Why Use XSD
- Define the data format and rules for creating XML documents
- Automatic validation before further processing
- Specify precise data types (e.g., date, number)
- Define required/optional elements and their occurrence
Well-formed vs Valid
- Well-formed: An XML document follows XML syntax (correct tag closing, escaping, etc.)
- Valid: An XML document also conforms to the defined XSD (structure and data types)
XSD File Structure
- Root element: xs:schema
- Namespace declaration: xmlns:xs="http://www.w3.org/2001/XMLSchema"
- Optional: targetNamespace, elementFormDefault (qualified/unqualified)
Definition: elementFormDefault = "qualified" means that local elements must be namespace-qualified in the instance.
Content Types
xs:simpleType
- Contains only text (no child elements) and may have restrictions (enumeration, pattern, length, minLength, maxLength, fractionDigits, totalDigits, whiteSpace)
- Used for both elements and attributes
Example of a simple element:
<xs:element name="age" type="xs:integer"/>
xs:complexType
- Elements with child elements, attributes, or both
- Content models: xs:sequence, xs:choice, xs:all
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" use="required"/>
</xs:complexType>
</xs:element>
## Order and Repetition of Elements
- <xs:sequence>: exact order
- <xs:choice>: exactly one of the listed elements
- <xs:all>: elements can be in any order, each 0 or 1 time
Occurrence Parameters: minOccurs, maxOccurs (e.g., maxOccurs="unbounded")
Occurrence Example:
```xml
<xs:element name="owner" type="xs:string" minOccurs="1" maxOccurs="unbounded"/>
## Attributes
- Declaration: <xs:attribute name="att_name" type="xs:string" use="optional|required" default="..." fixed="..."/>
- default: the parser supplies the value if the attribute is missing
- fixed: the value is fixed and cannot be changed
## Value Restrictions and Constraints
- enumeration, pattern (regex), length, minLength, maxLength, fractionDigits, totalDigits, minInclusive, maxInclusive, minExclusive, maxExclusive, whiteSpace
### Example of Enumeration and Pattern:
```xml
<xs:element name="color">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="red"/>
<xs:enumeration value="green"/>
<xs:enumeration value="blue"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="plate">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="[A-Z0-9]{7}"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
Definition:
<xs:pattern>uses Unicode regular expressions (Level 1). Patterns must not span multiple lines.
Namespaces and References
- To link an XSD to an XML instance, the
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"attribute is used, along withxsi:schemaLocation="namespace path/to/schema.xsd"orxsi:noNamespaceSchemaLocationwhen no targetNamespace is specified. elementFormDefaultandtargetNamespacedetermine which elements are namespace-qualified.
Example of linking:
<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.example.com/persons persons.x
Already have an account? Sign in
XML Schema (XSD) overview
Klíčové pojmy: XSD describes the structure and data types of XML documents, Difference: well-formed checks syntax, valid checks against an XSD, xs:simpleType for text values, xs:complexType for elements with children/attributes, Order: sequence, choice, all; repetition: minOccurs, maxOccurs, Attributes: use=optional|required, default vs. fixed, Restrictions: enumeration, pattern, length, min/maxInclusive/Exclusive, fractionDigits, Namespaces: xmlns:xsi and xsi:schemaLocation link XML with XSD, Design patterns: Russian Doll, Salami Slice, Venetian Blind, Group and attributeGroup for reusing sets of elements/attributes, Practical validation steps: well-formed, correct namespaces, tools like xmllint