XML Schema (XSD) is a language used to define the structure and content of XML documents. It is a modern alternative to DTD (Document Type Definition). XML Schema provides more powerful and flexible data validation mechanisms.
Main Features of XML Schema
- XML-based syntax: Schema itself is an XML document, easy to understand and process
- Rich data types: Supports multiple built-in data types such as strings, integers, dates, booleans, etc.
- Custom types: Can define complex types and simple types
- Namespace support: Native support for XML namespaces
- Inheritance and extension: Supports type inheritance and extension mechanisms
- Precise constraints: Can define element cardinality, value ranges, formats, and other constraints
Differences Between XML Schema and DTD
| Feature | XML Schema | DTD |
|---|---|---|
| Syntax | XML-based | Unique DTD syntax |
| Data types | Rich built-in types | Only string type |
| Namespaces | Native support | Not supported |
| Inheritance | Supports type inheritance | Not supported |
| Extensibility | Can extend and reuse | Difficult to reuse |
| Validation capability | Powerful validation capability | Limited validation capability |
XML Schema Basic Structure
xml<?xml version="1.0"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.example.com/books" xmlns="http://www.example.com/books" elementFormDefault="qualified"> <!-- Define elements --> <xs:element name="book" type="BookType"/> <!-- Define complex types --> <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:element name="publishDate" type="xs:date"/> </xs:sequence> <xs:attribute name="id" type="xs:string" use="required"/> </xs:complexType> </xs:schema>
Common Schema Elements
- element: Defines XML elements
- complexType: Defines complex types (containing child elements or attributes)
- simpleType: Defines simple types (containing only text content)
- attribute: Defines element attributes
- sequence: Specifies that child elements must appear in order
- choice: Specifies that only one of the child elements can appear
- all: Specifies that child elements can appear in any order
Constraint Definitions
xml<!-- Cardinality constraints --> <xs:element name="phone" minOccurs="0" maxOccurs="unbounded"/> <!-- Value range constraints --> <xs:simpleType name="AgeType"> <xs:restriction base="xs:integer"> <xs:minInclusive value="0"/> <xs:maxInclusive value="120"/> </xs:restriction> </xs:simpleType> <!-- Pattern constraints --> <xs:simpleType name="EmailType"> <xs:restriction base="xs:string"> <xs:pattern value="[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}"/> </xs:restriction> </xs:simpleType> <!-- Enumeration constraints --> <xs:simpleType name="GenderType"> <xs:restriction base="xs:string"> <xs:enumeration value="male"/> <xs:enumeration value="female"/> </xs:restriction> </xs:simpleType>
Referencing Schema in XML Documents
xml<?xml version="1.0"?> <book xmlns="http://www.example.com/books" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.example.com/books books.xsd"> <title>XML Programming</title> <author>John Doe</author> <price>49.99</price> <publishDate>2024-01-15</publishDate> </book>
Practical Application Scenarios
- Message format definition in web services (such as WSDL)
- Structure validation of configuration files
- Standardization of data exchange formats
- Definition and validation of document formats
XML Schema provides powerful and flexible XML document validation mechanisms and is an indispensable tool in modern XML application development.