Home | | Web Technology | XML Schema and its Data Types

Chapter: Web Technology : Web Services

XML Schema and its Data Types

The purpose of an XML Schema is to define the legal building blocks of an XML document. It is used to represent data types of an XML document.

XML Schema:


The purpose of an XML Schema is to define the legal building blocks of an XML document. It is used to represent data types of an XML document. It is an alternative to DTD (Document Type Definition). XML Schema defines elements, attributes and vales of elements and attributes.


An XML Schema:


defines elements that can appear in a document defines attributes that can appear in a document defines which elements are child elements


defines the order of child elements defines the number of child elements


defines whether an element is empty or can include text defines data types for elements and attributes


defines default and fixed values for elements and attributes


XML Schemas Data Types


One of the greatest strength of XML Schemas is the support for data types. Using XML schema it is easier to describe allowable document content; it is easier to validate the correctness of data; it is easier to convert data between different data types.



Built in data types supported by XML schema


XML Schema has a lot of built-in data types. The most common types are: xs:string








XML schema defines in the namespace http://www.w3.org/2001/XMLschema that contains built in data types. There are two classes of XML schema data types:


Complex type is a data type is represented using markup.


Simple type is a data type whose values are represented in XML doc by character data. XML markup such as <types> is known as XML schema that conforms to W3C defined XML schema vocabulary which defines all or part of the vocabulary for another XML document. The <schema> is the root element for any XML schema document. The child elements of schema define the data types.


Example for writing a simple XML Schema


Step 1: Write a xsd file in which the desired structure of the XML document is defined and named it as StudentSchema.xsd.


<?xml version=”1.0”?>


<xs:schema xmlns:xs=” http://www.w3.org/2001/XMLSchma”> <xs:element name=”student” >





<xs:element name=”name” value=”xs:string” />


<xs:element name=”regno” value=”xs:string” /> <xs:element name=”dept” value=”xs:string” /> </xs:sequence>






The xs qualifier used to identify the schema elements and its types. The xs:schema is the root element. It takes the attributes xmlns:xs which has the value, “http://www.w3.org/2001/XMLSchema. This declaration indicates that the document follows the rule of XMLSchema defined by W3 in year 2001. The xs:element is used to define the xml element. The Student element is complexType which has three child elements:name, regno and dept. All these elements are of simple type string.


Step 2: Write a XML document and reference the xsd file. Named it as myXML.xml <?xml version=”1.0”?>


<student xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”




<name> Raj </name> <regno>3212654556</regno> <dept>CSE</dept> </student>



The following fragment: xmlns:xsi=”http://www.w3.org/2001/XMLSchemainstance” tells the XML Schema Instance namespace available.


The following code, xsi:schemaLocation=”StudentSchema.xsd” is used to reference the XML schema document (xsd) file which contains the definition of this xml document. Various xml validation tools are available which can be used to validate xml and its schema document.




Supports data types Supports namespaces


XML schemas support a set of data types, similar to the ones used in most common programming languages


Provides the ability to define custom data types


Easy to describe allowable content in the document Easy to validate the correctness of data


Object oriented approach like inheritance and encapsulation can be used in creating the document


Easier to convert data between different data types Easy to define data formats


Easy to define restrictions on data


It is written in xml so any xml editor can be used to edit xml schema Xml Parser can be used to parse the schema file


XML schemas are more powerful than DTDs. Everything that can be defined by the DTD can also be defined by schemas, but not vice versa.




Complex to design and learn


Maintaining XML schema for large XML document slows down the processing of XML document DTD for the above xml file instead of XML schema


The purpose of a DTD (Document Type Definition) is to define the legal building blocks of an XML document.


A DTD defines the document structure with a list of legal elements and attributes.


Step 1: Create a DTD. Name it as student.dtd


<!ELEMENT student(name, regno, dept)> <!ELEMENT name(#PCDATA)> <!ELEMENT regno(#PCDATA)> <!ELEMENT dept(#PCDATA)>


!ELEMENT student defines that the note element contains three elements: "name.

regno, dept"


PCDATA means parsed character data which is the text found between the start tag and the end tag of an XML element.


Step 2: Write the XML document and reference the DTD file in it. Name it as myXML.xml


<?xml version=”1.0”?>


<!DOCTYPE student SYSTEM student.dtd> <student>

<name> Raj </name>






!DOCTYPE student defines that the root element of this document is student and links the myXML.xml file with the student.dtd file.

Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail
Web Technology : Web Services : XML Schema and its Data Types |

Privacy Policy, Terms and Conditions, DMCA Policy and Compliant

Copyright © 2018-2023 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.