An XML schema describes the Structure of the XML message.
The XML schema language is also called as XML Schema Definition (XSD).
An XML schema:

  1. Defines Data types for elements and attributes.
  2. Defines elements that can appear in a document.
  3. Defines attribute that can appear in a document.
  4. Defines child elements and their hierarchy.
  5. Defines the number of child elements and their sequence.
  6. Defines whether and element is empty or can include text.
  7. Defines default and fixed values for elements and attributes.

XML Schema’s are richer and powerful than DTD because:

  1. XML Schema’s are extensible to future additions
  2. XML Schema’s themselves are written in XML
  3. XML Schema’s support data types
  4. XML Schema’s support namespaces

XML Schema’s are extensible.
XML Schema’s are extensible, because they are written in XML.
WITH an Extensible Schema definition we can:
Reuse one Schema in other Schema’s.

  1. Create our own data types derived from the standard types.
  2. Reference multiple schema’s in the same document.

Data types
One of the greatest strength of XML Schema’s is the support for data types. By using data types, it is easier to:

  1. Describe allowable document content
  2. Validate the correctness of data
  3. work with data from a database
  4. Define data facets (restrictions on data)
  5. Define data patterns (data formats)

The XML Data types are:

  1. Simple types
  2. Complex types

XML Schema

  1. XML Schema defines element names and element types
  2. XML documents are validated against XML Schema.
  3. XML Schema’s are saved with xsd extension.


<?xml version=“1.0”?.
"<xs:schema xmlns:xs="" targetNameSpace=""" elementFormDefault="qualified"/>
<xs:element name="note">
	<xs:element name="to" type="xs:string"/>
	<xs:element name="from" type="xs:string"/>
	<xs:element name="heading" type="xs:string"/>
	<xs:element name="body" type="xs:string"/>

Theelement is the root element of every XML Schema.Theelement contains following.

XML Document

It is also called as XML Message or XML Document Fragmentand saved with .xml extension.
An XML Document refers XML Schema using ‘schemaLocation’ attribute, whose value is namespace followed by blank space followed by schema name.

<?xml version="1.0"?>
xsi:schemaLocation=" Note.xsd">
	<tns:body>Don’t forget me this weekend!</tns:body>

Simple Types
The Xml element is defined using simple types and contains only text without attributes or sub element.
All Built-in types (both Primitive types and Derived types) are Simple types.

The Build-in types with Restrictions are also Simple types.

Build-in types
The build-in types are:

  1. Primitive types
  2. Derived types

Primitive types
The following table summarizes Primitive types in XML Schema Definition:

Derived types
The following table summarizes derived types.

Built-in types with restrictions
The built-in types with restrictions are simple types
Restrictions are used to define acceptable values for XML elements or attributes. Restrictions on XML elements are called facets.

Restriction on values
The following example defines an element named “age” with a restriction. The value of the employee age cannot be lower than 21 and greater than 60.

<xs:element name="age">
	<xs:restriction base="xs:integer">
	<xs:miniInclusive value="21">
	<xs:maxiInclusive value="60">

Restriction on Set of values
To limit the content of an XML element to a set of acceptable values, we would use the enumeration constraint.

<xs:element name="car">
	<xs:restriction base="xs:string"/>
				<xs:enumeration value="Audi">
				<xs: enumeration value ="Golf"/>
		<xs: enumeration value ="BMW"/>

Restriction on Series of values

The ‘pattern’ constraint is used to limit the content of xml element to define a series of numbers of letters.
Example #1: The only acceptable value is THREE of the LOWERCASE or UPPERCASE letters from a to z or A to Z.

<xs:element name="initials">
	<xs:restriction base="xs:string">
		<xs:pattern value=" [a-zA-Z][a-zA-Z][a-zA-Z] "/>

Example #2: The acceptable value is zero or more occurrences of lowercase letters from a to z.

<xs:element name="letter">
	<xs:restriction base="xs:string">
		<xs:pattern value="([a-z])*"/>

Example #3: The only acceptable value is male OR female.

<xs:element name="gender">
	<xs:restriction base="xs:string">
		<xs:pattern value="male/female"/>

Restriction on Length

To limit the length of a value in an element, we would use the length, maxLength, and minLength constrains.
Example #1: This example defines an element called “password” with a restriction. The value must be exactly eight characters.

Example #2: This example defines another element called “password” with a restriction. The value must be minimum five characters and maximum eight characters.

<xs:element name="password">
	<xs:restriction base="xs:string">
		<xs:minlength value="5"/>
		<xs:maxlength value="8"/>