Schema in XML
XML Schema
What is XML Schema?
An XML Schema defines the structure, content, and data types of XML documents.
It is a more powerful and flexible alternative to DTD (Document Type Definition).
XML Schema uses XML syntax itself (usually with
.xsdextension).
Purpose of XML Schema
To validate XML documents for correctness.
To specify data types (e.g., string, integer, date).
To define element and attribute structure and restrictions.
To allow namespaces and more complex rules.
Basic Components of XML Schema
Elements: Define what elements are allowed and their types.
Attributes: Define attributes and their data types.
Complex Types: Define elements containing other elements (nested).
Simple Types: Define elements with text-only content and data types.
Facets: Restrictions like length, pattern, enumeration.
Example XML Schema (XSD)
<?xml version="1.0" encoding="UTF-8"?><xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:element name="person"> <xs:complexType> <xs:sequence> <xs:element name="name" type="xs:string"/> <xs:element name="age" type="xs:int"/> </xs:sequence> <xs:attribute name="id" type="xs:int" use="required"/> </xs:complexType> </xs:element></xs:schema>Example XML Document Validated by Above Schema
<person id="1"> <name>Vikash Gupta</name> <age>30</age></person>Benefits of XML Schema
Uses standard XML format (easier to read and edit).
Supports data types and value restrictions.
Supports namespaces.
Provides better validation than DTD.
Summary
| Feature | XML Schema (XSD) | DTD |
|---|---|---|
| Format | XML | Custom syntax |
| Data Types | Yes (string, int, date, etc.) | No |
| Namespaces Support | Yes | No |
| Validation Power | More powerful | Limited |
If you want, I can help you write XML Schemas or validate XML files using Schema!