XML — Schema
What is XSD?
XML Schema Definition (XSD) defines the structure and data types of XML documents.
Basic Schema
<?xml version="1.0" encoding="UTF-8"?>
<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:element name="price" type="xs:decimal"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
Data Types
| Type | Description |
|---|---|
| xs:string | Text |
| xs:integer | Whole number |
| xs:decimal | Decimal number |
| xs:boolean | true/false |
| xs:date | Date |
| xs:dateTime | Date and time |
Restrictions
<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>
Occurrence
<xs:element name="book" maxOccurs="unbounded"/>
<xs:element name="subtitle" minOccurs="0"/>
Referencing a Schema
<book xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="book.xsd">
Mini Practice
- Create a basic XSD
- Use different data types
- Add restrictions and constraints
- Validate XML against the schema
Up Next
Continue with Parser — parsing XML documents.
Related Topics
Frequently Asked Questions about Schema
What is Schema in XML?
Schema is a fundamental concept in XML. This lesson explains it step by step with clear examples, making it easy for beginners to understand.
How do I learn Schema?
Start by reading the explanation above, then try the code examples. Practice by modifying the examples and experimenting with different values. Hands-on practice is the best way to learn Schema.
Why is Schema important in XML?
Schema is essential for XML development. Understanding this concept will help you write better code and solve real-world problems more effectively.