XML schema or XML Schema Definition (XSD) defines the structure of XML documents. Schema information defines the elements and attributes that are used in the document. It includes the order, number and type of elements.

Difference between DOCTYPE and Schema

XML schema is alternative to DTD and includes much more features than DTD. The XML Schema files can be edited and parsed in XML parser. They can be used in other schema files as well. It is possible to create own data types which are combination of standard data type elements.

Consider the XML file below with elements defined in DTD:

File1.dtd

<!DOCTYPE item[

      <!ELEMENT item (name, cost, material)*>

      <!ELEMENT name (#PCDATA)>

      <!ELEMENT cost (#PCDATA)>

      <!ELEMENT material (#PCDATA)>

]>

File1.xml

<?xml version="1.0"?>

<!DOCTYPE item SYSTEM "File1.dtd">

<item>

  <name>Blanket</name>

  <cost>5000</cost>

  <material>Wool</material>

</item>

An XML file elements defined with schema is as below:

SchemaFile2.xsd

<?xml version="1.0"?>

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">

<xs:element name="item">

  <xs:complexType>

    <xs:sequence>

      <xs:element name="name" type="xs:string"/>

      <xs:element name="cost" type="xs:decimal"/>

      <xs:element name="material" type="xs:string"/>

    </xs:sequence>

  </xs:complexType>

</xs:element>

</xs:schema>

The schema file has the extension .xsd. The root element of schema file starts with <xs:schema>. The xmlns:xs=http://www.w3.org/2001/XMLSchema defines the elements and datatypes comes from that namespace. User can add default namespace for the file along with this declaration.

SchemaFile2.xml

<?xml version="1.0"?>

<item

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

xsi:schemaLocation="schemaDemo2.xsd">

  <name>Blanket</name>

  <cost>5000</cost>

  <material>Wool</material>

</item>

Declaring elements

Elements define the structure of XML document. Element can contain another element or text or it can be empty. Element can be defined as simple or complex types. This topic is discussed in detail later.

Elements are defined in XSD as follows:

Syntax for XML schema elements

<xs:element name=”xxx” type=”yyy”>

Example of XML schema elements

<xs:element  name=”UNO” type=”xs:string” fixed=”United Nations Organisation” />

<xs:element  name=”emp_ID” type=”xs:integer” minOccurs=”0” maxOccurs=”unbounded” />

Element has name and type properties. The name defines the name of an element, and type defines the data type of an element.

The predefined datatypes are:

  • xs:string,
  • xs:integer,
  • xs:boolean,
  • xs:date,
  • xs:time,
  • xs:decimal.

The user defined element types use <xs:simple type> or <xs:complex type> as tags. The element can have default or fixed properties.  The default  gives the value assigned to the element if it’s value is not specified. The fixed assigns its value to the element and user cannot change it. If an element has to occur multiple times, the minOccurs and maxOccurs can be used. The default value for these attributes is 1. It can contain any positive value from 0, 1, 2 … etc. To specify maximum, the string unbounded is used.

Declaring attributes

Attributes are defined as complex type elements. Simple elements do not have attributes. Attributes are defined in XSD as follows:

Syntax for XML schema attributes

<xs:attribute name=”xxx” type=”yyy”>

Example of XML schema attributes

<xs:attribute  name=”lang” type=”xs:string” default=”EN” use=”required”/>

Attribute has name and type properties. The name defines the name of attribute, and type defines the data type of attribute.

The predefined datatypes are:

  • xs:string,
  • xs:integer,
  • xs:boolean,
  • xs:date,
  • xs:time,
  • xs:decimal.

An attribute can have default or fixed properties.  The default gives the value assigned to the attribute if it’s value is not specified. The fixed assigns it’s value to the attribute and user cannot change it. If the attribute is required for an element, use field is used. If it is not used, attributes are optional by default.

 

›› go to examples ››