XML Schema (XSD):
Introduction to XML Schema for Defining XML Document Structure:
An XML Schema (XSD) is a more powerful and flexible way to define the structure, content, and data types of an XML document compared to DTD.
XML Schema uses XML code to define structures and features such as data types and namespaces and many others.
Key Features of XML Schema:
- Defines elements and attributes with data types.
- Supports complex data structures and relationships.
- Provides data validation and ensures data integrity.
Example:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="bookstore">
<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:element name="price" type="xs:decimal"/>
</xs:sequence>
<xs:attribute name="id" type="xs:string" use="required"/>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
Basic XSD Elements: simpleType, complexType, elements, and attributes
Elements:
Defined using <xs:element>, specifying the element name and type.
Example:
<xs:element name="title" type="xs:string"/>
simpleType:
Used to define simple data types for elements and attributes.
Example:
<xs:simpleType name="priceType">
<xs:restriction base="xs:decimal">
<xs:minInclusive value="0.0"/>
</xs:restriction>
</xs:simpleType>
complexType:
Used to define complex elements that contain other elements and/or attributes.
Example:
<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:sequence>
<xs:attribute name="id" type="xs:string" use="required"/>
</xs:complexType>
Attributes:
Defined using <xs:attribute>, specifying the attribute name and type.
Example:
<xs:attribute name="id" type="xs:string" use="required"/>
Data Types and Constraints in XSD
Data Types:
XML Schema provides built-in data types, which can be classified into two categories: two classifications: elements and compounds.
- Simple Types: Used for elements and attributes that contain only text.
- xs:string
- xs:integer
- xs:decimal
- xs:date
- xs:boolean
Example:
<xs:element name="price" type="xs:decimal"/>
Complex Types: Used for elements that contain other elements and/or attributes.
Example:
<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:sequence>
</xs:complexType>
Constraints:
Constraints are used to define rules for element and attribute values.
- Restrictions: Define constraints on simple types.
Example:
<xs:simpleType name="positiveInteger">
<xs:restriction base="xs:integer">
<xs:minInclusive value="1"/>
</xs:restriction>
</xs:simpleType>
- Length, Pattern, and Enumeration: Define specific constraints.
Example:
<xs:simpleType name="isbnType">
<xs:restriction base="xs:string">
<xs:length value="13"/>
<xs:pattern value="\d{13}"/>
</xs:restriction>
</xs:simpleType>
Example of a Complete XSD with Constraints:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="bookstore">
<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:element name="price" type="xs:decimal"/>
</xs:sequence>
<xs:attribute name="id" type="xs:string" use="required"/>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>