XML Schema

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

<xs: schema xmlns:xs="http://www.w3.org/2001/XML Schema">

Example of schema file.

student.xsd
<?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
<?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
Simple Type
Complex type
Simple Type use only text based elements. It has some pre-defined simple types are:
xs:integer
xs:boolean
xs:string
xs:date

Example

<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
<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>



ADVERTISEMENT
ADVERTISEMENT