XML Scheme commonly known as XML Schema Definition. It is used to describe the elements and attribute for a class of XML document. They can also specifies the structure of an XML document. It is similar to a database schema that describes the data in a database.
Syntax
1 2 3 |
<xs: schema xmlns:xs="http://www.w3.org/2001/XML Schema"> |
Example of schema file.
student.xsd
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<?xml version="1.0"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="https://www.tutorialandexample.com" xmlns="https://www.tutorialandexample.com" elementFormDefault="qualified"> <xs:element name="student"> <xs:complexType> <xs:sequence> <xs:element name="firstname" type="xs:string" /> <xs:element name="lastname" type="xs:string" /> <xs:element name="address" type="xs:string" /> </xs:sequence> </xs:complexType> </xs:element> </xs:schema> |
Now see the xml file using XSD file
student.xml
1 2 3 4 5 6 7 8 9 10 11 |
<?xml version="1.0"?> <student xmlns="https://www.tutorialandexample.com" xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance xsi:schemaLocation="https://www.tutorialandexample.com student.xsd"> <firstname>Ajay</firstname> <lastname>Singh</lastname> <address>2/4 A Minto road Civil lines Allahabad</address> </student> |
Types of Scheme
There are two types of XML Scheme
1 2 3 4 |
Simple Type Complex type |
Simple Type use only text based elements. It has some pre-defined simple types are:
1 2 3 4 5 6 |
xs:integer xs:boolean xs:string xs:date |
Example
1 2 3 4 5 |
<xs:element name="Firstname" type="xs:string"/> <xs:element name="Lastname" type="xs:string"/> <xs:element name="Age" type="xs:integer"/> |
Complex type
It is used to hold multiple attributes and elements. It contains additional sub elements and can be left empty.
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<xs: element name="Teacher"> <xs: complexType> <xs: sequence> <xs: element ref="Firstname" /> <xs: element ref="Lastname" /> <xs: element ref="Age" /> </xs: sequence> <xs : attribute ref="Title" use="optional"/> </xs: complexType> </xs: element> </xs: schema> |