Before
writing XML schema, let’s understand basic concepts.
1. Simple Type Element
An XML
element with only text data (No attributes, no child elements) is called Simple
type element.
Example
<salary>70000</salary>
<name>Krishna</name>
Above
two are simple type elements.
<designation
experience=”4Yrs”>Software Developer</designation>
Above
element is not simple type element, since it has attribute “experience”.
2. Complex Element
Element
other than simple type is considered to be complex type element.
Example
<employee> Employee Details <name>Krishna</name> <salary currency="rupees">80000</salary> </employee>
In the
above example, elements employee and salary are complex type elements, whereas
name and currency are simple elements.
<?xml version="1.0" encoding="UTF-8"?> <employees> <employee> <id>1</id> <name> <firstname>Hari</firstname> <middlename>Krishna</middlename> <lastname>Gurram</lastname> <age>25</age> <salary>80000</salary> <married>single</married> </name> <project> <department>aero</department> <pjtname>Flight Controls</pjtname> <from>2011-09-16</from> <to>2013-01-22</to> </project> <project> <department>banking</department> <pjtname>Online Transaction Processing System</pjtname> <from>2013-01-23</from> <to>2014-10-06</to> </project> <project> <department>mobile</department> <pjtname>Daily News Application</pjtname> <from>2014-10-07</from> <to>2015-05-06</to> </project> </employee> </employees>
Consider
the above example,
Elements in employee.xml are
employees,
employee, id, name, firstname, middlename, lastname, age, salary, married, project,
department, pjtname, from, to.
Let’s
differentiate simple and complex elements in employee.xml
Simple elements in employee.xml are
id, firstname,
middlename, lastname, age,salary, married, department, pjtname, from, to are
simple elements, since they contains only text data (No attributes and child
elements).
Complex elements in employee.xml are
employees,
employee, name, project are complex elements.
it is
time to write XML Schema file.
Step1: Create employee.xsd file. ‘xsd’
stands for XML Schema Definition and it is a w3c recommendation to specify how to formally describe the elements in an
Extensible Markup Language(XML).
Step 2: Create schema element in
employee.xsd file.
<?xml version="1.0" encoding="UTF-8"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> </xs:schema>
As you
observe, xsd document itself XML document, so above statement contains xml
declaration on top.
‘xmlns:xs="http://www.w3.org/2001/XMLSchema"
'
Above
snippet indicates that the elements and data types used in the schema come from
the "http://www.w3.org/2001/XMLSchema" namespace. It also specifies
that the elements and data types that come from the
"http://www.w3.org/2001/XMLSchema" namespace should be prefixed with xs.
Step
3: Define simple elements.
Syntax
to define simple element
<xs:element
name="nameOf TheElement " type="datatype" />
XML
schema comes with lot of built in type like string, decimal, integer, boolean,
date, time etc., we will discuss about data types later.
Definitions
for simple elements looks like below.
<!-- Schema for simple elements --> <xs:element name="id" type="xs:integer" /> <xs:element name="firstname" type="xs:string" /> <xs:element name="middlename" type="xs:string" /> <xs:element name="lastname" type="xs:string" /> <xs:element name="age" type="xs:integer" /> <xs:element name="salary" type="xs:decimal" /> <xs:element name="married" type="xs:string" /> <xs:element name="department" type="xs:string" /> <xs:element name="pjtname" type="xs:string" /> <xs:element name="from" type="xs:date" /> <xs:element name="to " type="xs:date" />
xs:string,
xs:integer, xs:date are predefined data types of XML Schema.
Step 3: Define complex elements.
We
define schema for below snippet.
<project> <department>aero</department> <pjtname>Flight Controls</pjtname> <from>2011-09-16</from> <to>2013-01-22</to> </project>
Schema
for the element project looks like below.
<!-- Schema for element project --> <xs:element name="project"> <xs:complexType> <xs:sequence> <xs:element ref="department" /> <xs:element ref="pjtname" /> <xs:element ref="from" /> <xs:element ref="to" /> </xs:sequence> </xs:complexType> </xs:element>
<xs:complexType>: Defines a complex type, which determines the set of attributes and the content of an element.
<xs:sequence> : Specifies elements in the group to appear in the specified sequence within the containing element.
<xs:element ref="pjtname"/>: ‘ref’ attribute specifies the name of an element declared in this schema.
Complete employees.xsd file looks like below.
<?xml version="1.0" encoding="UTF-8"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> <!-- Schema for simple elements --> <xs:element name="id" type="xs:integer" /> <xs:element name="firstname" type="xs:string" /> <xs:element name="middlename" type="xs:string" /> <xs:element name="lastname" type="xs:string" /> <xs:element name="age" type="xs:integer" /> <xs:element name="salary" type="xs:decimal" /> <xs:element name="married" type="xs:string" /> <xs:element name="department" type="xs:string" /> <xs:element name="pjtname" type="xs:string" /> <xs:element name="from" type="xs:date" /> <xs:element name="to " type="xs:date" /> <!-- Schema for element name --> <xs:element name="name"> <xs:complexType> <xs:sequence> <xs:element ref="firstname" /> <xs:element ref="middlename" /> <xs:element ref="lastname" /> <xs:element ref="age" /> <xs:element ref="salary" /> <xs:element ref="married" /> </xs:sequence> </xs:complexType> </xs:element> <!-- Schema for element project --> <xs:element name="project"> <xs:complexType> <xs:sequence> <xs:element ref="department" /> <xs:element ref="pjtname" /> <xs:element ref="from" /> <xs:element ref="to" /> </xs:sequence> </xs:complexType> </xs:element> <!-- Schema for element employee --> <xs:element name="employee"> <xs:complexType> <xs:sequence> <xs:element ref="id" /> <xs:element ref="name" /> <xs:element ref="project" maxOccurs="unbounded" /> </xs:sequence> </xs:complexType> </xs:element> <!-- Schema for element employees --> <xs:element name="employees"> <xs:complexType> <xs:sequence> <xs:element ref="employee" maxOccurs="unbounded" /> </xs:sequence> </xs:complexType> </xs:element> </xs:schema>
Link employees.xsd file to
employees.xml file
<employees
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="employees.xsd">
Update
employees tag like above in employees.xml file.
employee.xml
<?xml version="1.0" encoding="UTF-8"?> <employees xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="employees.xsd"> <employee> <id>1</id> <name> <firstname>Hari</firstname> <middlename>Krishna</middlename> <lastname>Gurram</lastname> <age>25</age> <salary>80000</salary> <married>single</married> </name> <project> <department>aero</department> <pjtname>Flight Controls</pjtname> <from>2011-09-16</from> <to>2013-01-22</to> </project> <project> <department>banking</department> <pjtname>Online Transaction Processing System</pjtname> <from>2013-01-23</from> <to>2014-10-06</to> </project> <project> <department>mobile</department> <pjtname>Daily News Application</pjtname> <from>2014-10-07</from> <to>2015-05-06</to> </project> </employee> </employees>
You can
validate employees.xml file now.
You can
validate the document online also. Goto below site and validate your data.
Prevoius Next Home
No comments:
Post a Comment