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

11/03/2015 Categories: XML. No Comments on XML Schema

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

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:

 

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:

 

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:

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

 

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:

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:

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

 

Suryateja Pericherla

Suryateja Pericherla

Hello, I am Suryateja Pericherla working as an Asst. Professor in CSE department at Vishnu Institute of Technology. I write articles to share my knowledge and make people knowledgeable regarding certain topics.
Suryateja Pericherla

Latest posts by Suryateja Pericherla (see all)

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

Scroll Up
Thank you for visiting startertutorials.com! If you like this website do share with your friends by clicking on the social icons available on the left hand side.

As the domain name is quite large to remember, use this short domain to visit this site. Short domain: stuts.me