Advanced Java and Web Technologies for JNTUK
Blog providing beginner tutorials on different web technologies like HTML, CSS, Javascript, PHP, MYSQL, XML, Java Beans, Servlets, JSP and AJAX
Subscribe to Startertutorials.com's YouTube channel for different tutorial and lecture videos.

Categories: XML. No Comments on XML Schema
0
(0)

This article explains XML Schema which is an alternative for DTDs in specifying the high level syntax for an XML document. We will learn creating and using XML Schema along with XML documents.

 

Introduction

 


Subscribe to our monthly newsletter. Get notified about latest articles, offers and contests.


Like DTD, a XML Schema also specifies the structure of the tags and attributes in a XML document. Why XML Schema when there is already DTD?

 

There are several disadvantages of using DTD for specifying the structure of a XML document. First disadvantage is, DTDs syntax is different from that of XMLs syntax. We have to learn new syntax to work with DTDs. Second disadvantage is DTD supports less data types (ten types) for controlling the values of tags and attributes.

 

In order to overcome these disadvantages, one of the alternatives proposed by W3C is XML Schema.

 

Differences between DTD and XML Schema are listed below:

  • XML schemas are written in XML while DTD are derived from SGML syntax.
  • XML schemas define data types for elements and attributes while DTD doesn’t support data types.
  • XML schemas allow support for namespaces while DTD does not.
  • XML schemas define number and order of child elements, while DTD does not.
  • XML schemas can be manipulated on your own with XML DOM but it is not possible in case of DTD.
  • For using XML schema there is no need to learn new syntax but working with DTD requires learning new syntax.
  • XML schema provides secure data communication i.e sender can describe the data in a way that receiver will understand, but in case of DTD data can be misunderstood by the receiver.
  • XML schemas are extensible while DTD is not extensible.

 

Defining a Schema

 

Schemas are written using a collection of names from a namespace that is, in effect, a schema of schemas. The name of this namespace is http://www.w3.org/2001/XMLSchema. Some of the names available in this namespace are element, schema, sequence, and string.

 

Every schema has its root element as schema. The schema element specifies the namespace from which we can use the predefined names to write our schema. This namespace is specified using the attribute xmlns. This attribute also often specifies a prefix that should be used by the elements in the XML document. The namespace is specified as shown below:

xmlns:xsd = “http://www.w3.org/2001/XMLSchema”

 

A schema defines a namespace in the same sense as a DTD defines a tag set. The name of the namespace must be specified with the targetNamespace attribute of the schema element as shown below:

targetNamespace = “http://www.startertutorials.com/studentSchema”

 

If all the elements and attributes that are nested inside top-level elements have to be included in the target namespace, set elementFormDefault attribute on schema element to qualified as shown below:

elementFormDefault = “qualified”

 

The default namespace, which is the source of the unprefixed names in the schema, is given with another xmlns specification as shown below:

xmlns = “http://www.startertutorials.com/studentSchema”

 

An example for the complete schema opening tag is given below:

<xsd:schema
      xmlns:xsd = "http://www.w3.org/2001/XMLSchema"
      targetNamespace = "http://www.startertutorials.com/studentSchema"
      xmlns = "http://www.startertutorials.com/studentSchema"
      elementFormDefault = "qualified">

 

Defining a Schema Instance

 

A XML document which adheres to the schema is known as a schema instance. An instance of a schema must include specifications of the namespaces it uses. These are given as attribute assignments in the tag for its root element.

 

First attribute, xmlns defines the default namespace to be the one defined in its schema as shown below:

<students  xmlns = “http://www.startertutorials.com/studentSchema”>

 

Second attribute, specifies the standard namespace for instances, which is XMLSchema-instance as shown below:

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

 

Third attribute, schemaLocation specifies the location of the default namespace as shown below:

xsi: schemaLocation = “http://www.startertutorials.com/studentSchema students.xsd”

 

An example for the complete declaration of the opening tag students is given below:

<students 
      xmlns = "http://www.startertutorials.com/studentSchema"
      xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation = "http://www.startertutorials.com/studentSchema students.xsd">

 

Data Types in XML Schema

 

The user-defined types in XML schema can be divided into two types: simple and complex. A simple data type is one whose content is restricted to strings. A simple type cannot have attributes and nested elements. A complex type can have attributes and include other data types as elements.

 

XML schema defines 44 data types, 19 of which are primitive and 25 are derived types. The primitive data types include string, Boolean, float, time etc. The predefined derived types include byte, long, decimal, unsignedInt, positiveInteger etc.

 

User-defined types are defined by specifying restrictions on existing types. These constraints are given in terms of the facets of the base type. For example, the integer primitive data type has eight possible facets: totalDigits, maxInclusive, maxExclusive, minInclusive, minExclusive, pattern, enumeration, and whitespace.

 

Both simple and complex types can be named or anonymous. If anonymous, a type cannot be used outside the element in which it is declared.

 

Data declarations in a XML Schema can be either local or global. A local declaration is one that appears inside an element that is a child of the schema element. A global declaration is one that appears as a child of the schema element. Global elements are visible in the whole schema in which they are declared.

 

Simple Types

 

An element can be created in XML Schema using the element tag as shown below:

<xsd:element  name=”students”  type=”xsd:string” />

 

The name attribute specifies the name of the element and type attribute specifies the data type of the element. A default value can be included as shown in the below example:

<xsd:element  name=”class-strength”  type=”xsd:decimal”  default=”0″ />

 

Elements with a constant value can be created as shown below:

<xsd:element  name=”class-strength”  type=”xsd:decimal”  fixed=”60″ />

 

Attributes declarations must be placed at the end. Attributes can be created only if the element is a complex type. An attribute can be created as shown below:

<xsd:attribute  name=”id”  type=”xsd:decimal”  use=”required” />

 

User-defined types can be either simple types or complex types. A simple user-defined type can be created using the simpleType element. The base type for the user-defined type is specified using the restriction element and various facets. A facet is specified as an element along with value attribute.

 

An example of simple type, firstName of a person which can contain only 10 characters is as shown below:

<xsd:simpleType  name="firstName">
   <xsd:restriction  base="xsd:string">
      <xsd:maxLength  value="10" />
   </xsd:restriction>
</xsd:simpleType>

 

Another example of a simple type, phoneNumber which contains 10 digits only is shown below:

<xsd:simpleType  name="phoneNumber">
   <xsd:restriction  base="xsd:decimal">
      <xsd:precision  value="7" />
   </xsd:restriction>
</xsd:simpleType>

 

Complex Types

 

User-defined types which contains nested elements or attributes are known as complex types. A complex user-defined type can be created using the complexType tag.

 

The elements in a complex type can be ordered or unordered. An ordered group of elements is specified by the sequence tag as shown below:

<xsd:complexType>
   <xsd:sequence>
      <xsd:element  name="name"  type="xsd:string" />
      <xsd:element  name="branch" type="xsd:string" />
      <xsd:element  name="section" type="xsd:string" />
      <xsd:element  name="age"  type="xsd:decimal" />
   </xsd:sequence>
</xsd:complexType>

 

An unordered group of elements is specified by the all tag and the no. of occurrences of an element can be specified by using minOccurs and maxOccurs attributes as shown below:

<xsd:complexType>
   <xsd:all>
      <xsd:element  name="name"  type="xsd:string" />
      <xsd:element  name="branch" type="xsd:string" />
      <xsd:element  name="section" type="xsd:string" minOccurs="1" />
      <xsd:element  name="age"  type="xsd:decimal" />
      <xsd:element  name="email" type="xsd:string"  maxOccurs="unbounded" />
   </xsd:all>
</xsd:complexType>

 

An example for valid XML document and its schema is given below:

//students.xsd - Schema file
<?xml version="1.0" encoding="utf-8"?>
<xsd:schema
    xmlns:xsd = "http://www.w3.org/2001/XMLSchema"
    targetNamespace = "http://www.startertutorials.com/studentSchema"
    xmlns = "http://www.startertutorials.com/studentSchema"
    elementFormDefault = "qualified">
    <xsd:element name="students">
        <xsd:complexType>
            <xsd:sequence>
                <xsd:element name="student" maxOccurs="unbounded">
                    <xsd:complexType>
                        <xsd:sequence>
                            <xsd:element name="name" type="xsd:string" />
                            <xsd:element name="branch" type="xsd:string" />
                            <xsd:element name="age" type="xsd:decimal" />
                        </xsd:sequence>
                        <xsd:attribute name="id" type="xsd:decimal" use="required" />
                    </xsd:complexType>
                </xsd:element>
            </xsd:sequence>
        </xsd:complexType>
    </xsd:element>
</xsd:schema>

 

//student.xml - XML document
<?xml version="1.0" encoding="utf-8"?>
<students
    xmlns = "http://www.startertutorials.com/studentSchema"
    xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation = "http://www.startertutorials.com/studentSchema 
    students.xsd">
    <student id="1">
        <name>K.Ramesh</name>
        <branch>CSE</branch>
        <age>21</age>
    </student>
    <student id="2">
        <name>M.Suresh</name>
        <branch>CSE</branch>
        <age>21</age>
    </student>
</students>

 

 

How useful was this post?

Click on a star to rate it!

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?

Suryateja Pericherla

Suryateja Pericherla, at present is a Research Scholar (full-time Ph.D.) in the Dept. of Computer Science & Systems Engineering at Andhra University, Visakhapatnam. Previously worked as an Associate Professor in the Dept. of CSE at Vishnu Institute of Technology, India.

He has 11+ years of teaching experience and is an individual researcher whose research interests are Cloud Computing, Internet of Things, Computer Security, Network Security and Blockchain.

He is a member of professional societies like IEEE, ACM, CSI and ISCA. He published several research papers which are indexed by SCIE, WoS, Scopus, Springer and others.

Note: Do you have a question on this article or have a suggestion to make this article better? You can ask or suggest us by filling in the below form. After commenting, your comment will be held for moderation and will be published in 24-48 hrs.

Leave a Reply

Your email address will not be published. Required fields are marked *