XSD Introduction:-
XSD stands for XML Schema Design. It a file which provides
tags, attributes and their rules to construct an XML. In simple
words, XSD is a standard format for an XML.
XSD Contains two types of elements. They are
1) Simple Elements : These are the elements , which will never have any
child elements or any attributes.
2) Complex Elements: These are the elements which contains either
child or attribute , sometimes both.
Creating simple elements : A simple elements contains name and
data types. And we can even apply restrictions over the simple type.
Syntax to create a simple element:
<xs:element name="" type=""/>
or
<xs:element name="" type=""></xs:element>
Hear name indicates element name and type indicates element
data type.
ex: create an XML element empId as int type using XSD.
XSD:
<xs:element name="empId" type="xs:int"/>
We can use some other data types like
xs:int, xs:integer, xs:string,xs:decimal,xs:double etc..
Some other examples:
<xs:element name="empname" type="string"/>
<xs:element name="empsal" type="xs:decmal"/>
=========
Restrictions over Simple types:
empId element created as int type , it allows negative numbers and
positive numbers. But If i want to allow employee Id's from 100 to
300 then specify restrictions on input type.
Restrictions are given as:
1) Numeric/Integer restrictions :
minInclusive ,maxInclusive,minExclusive,maxExclusive.
Ex: crate an element voterAge that should accept only minimun 18
and Max 100 only.
<xs:element name="voterAge">
<xs:simpleType>
<xs:restriction base="xs:int">
<xs:minInclusive value="18"/>
<xs:maxInclusive value="100"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
or
<xs:element name="voterAge">
<xs:simpleType>
<xs:restriction base="xs:int">
<xs:minExclusive value="17"/>
<xs:maxExclusive value="101"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
2) String length Restrictions:-
minLength,maxLength,length(exact length).
ex: create an element empName , that should accept minimum 5
characters and max 20 characters
ex:
<xs:element name="empName">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:minLength value="5"/>
<xs:maxLength value="20"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
ex: create an element empName , that should accept exact 10
characters
<xs:element name="empName">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:length value="20"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
3) Decimal restrictions :
totalDigits,fractionDigits.
ex: Create a decimal number element that should accept at max 12
numbers and in that after decimal at max 3 numbers.
========
2) Complex Type elements:
ex: Design an complex type element that should accept two simple
elements
with given order
<employee>
<empId>101</empId>
<empName>ABCD</empName>
</employee>
XSD Code:
<xs:element name="employee">
<xs:complexType>
<xs:sequence>
<xs:element name="empId" type="xs:int"/>
<xs:element name="empName" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
External declaration : We can declare the complex/simple type externally and
can be referred as data types. They are known as targetNamespace(tns) data
types.
ex:
<xs:complexType name="emptypeOne">
<xs:sequence>
<xs:element name="empId" type="xs:int" />
<xs:element name="empName" type="xs:string" />
</xs:sequence>
</xs:complexType>
<xs:element name="employee" type="tns:emptypeOne" />
==================
Simple Type :Internal Declaration:
<xs:element name="empSal">
<xs:simpleType>
<xs:restriction base="xs:decimal">
<xs:totalDigits value="25" />
<xs:fractionDigits value="3" />
</xs:restriction>
</xs:simpleType>
</xs:element>
External Declaration :
<xs:simpleType name="empSaltype">
<xs:restriction base="xs:decimal">
<xs:totalDigits value="25"/>
<xs:fractionDigits value="3"/>
</xs:restriction>
</xs:simpleType>
<xs:element name="empSal" type="tns:empSaltype"/>