Advanced Web Technologies


XML Schema Definition


Objectives

The objective of this unit is the learner should able to understand the XML Schema Definition and its purpose. The learner should able to create valid XML documents using Schema.


3.1 Introduction

XSD (XML Schema Definition), a recommendation of the World Wide Web Consortium (W3C), specifies how to formally describe the elements in an Extensible Mark-up Language (XML) document. It can be used to verify each piece of item content in a document whether it adheres to the description of the element it is placed in. Like all XML schema languages, XSD can be used to express a set of rules to which an XML document must conform in order to be considered "valid" according to that schema. The XSD specification acknowledges the influence of DTDs and other early XML schema efforts such as DDML, SOX, XML-Data, and XDR.

XML Schema are primarily used to validate the structure and data types of an XML document. By structure we mean that an XML Schema defines:

  • what data elements are expected

  • what order the data elements are expected in

  • what nesting these data elements have

  • what data elements are optional and what data elements are required

XML Schema can also be used by XML data mapping tools for quickly extracting data from databases and transferring them in XML files.


3.2 Advantages of XML Schema

  • Names of elements and attributes are namespace-aware

  • Constraints can be defined for the textual content of elements and attributes, for example to specify that they are numeric or contain dates. A wide repertoire of simple types are provided as standard, and additional user-defined types can be derived from these, for example by specifying ranges of values, regular expressions, or by enumerating the permitted values.

  • Facilities for defining uniqueness constraints and referential integrity are more powerful: unlike the ID and IDREF constraints in DTDs, they can be scoped to any part of a document, can be of any data type, can apply to element as well as attribute content, and can be multi-part.
  • Many requirements that are traditionally handled using parameter entities in DTDs have explicit support in XSD: examples include substitution groups, which allow a single name (such as "block" or "inline") to refer to a whole class of elements; complex types, which allow the same content model to be shared by multiple elements; and model groups and attribute groups, which allow common parts of component models to be defined in one place and reused.
  • XSD 1.1 adds the ability to define arbitrary assertions (using XPath expressions) as constraints on element content.
  • XSD schema are conventionally written as XML documents, so familiar editing and transformation tools can be used.
  • XSD allows XML instances to be annotated with type information which is designed to make manipulation of the XML instance easier in application programs. 


3.3 XML Schema components

A schema is an abstract collection of metadata, consisting of a set of schema components mainly element and attribute declarations and complex and simple type definitions.

Schema documents are organized by namespace: all the named schema components belong to a target namespace, and the target namespace is a property of the schema document as a whole.

 The main components of a schema are:

  • Elements: Define properties of elements. These include the element name and target namespace. An important property is the type of the element, which constrains what attributes and children the element can have. In XSD 1.1, the type of the element may be conditional on the values of its attributes. Element declarations may be global or local, allowing the same name to be used for unrelated elements in different parts of an instance document.

  • Attributes: Define properties of attributes. Again the properties include the attribute name and target namespace. The attribute type constrains the values that the attribute may take. An attribute declaration may also include a default value or a fixed value (which is then the only value the attribute may take.)

  • Simple and complex types.

  • Model group and attribute group definitions. These are essentially macros: named groups of elements and attributes that can be reused in many different type definitions.

  • Annotations

  • Assertions

  • Notations


3.4 XML Schema Document

XML Schema begin with schema element which is the root element of your XML Schema

 The syntax of schema element is

             <xs:schema> .....  </xs:schema>

 schema element should contain the following important attributes in its declaration

  •  xmlns:xs="http://www.w3.org/2001/XMLSchema"
  • targetNamespace="https://www.example.com"
  • xmlns="https://www.example.com"
  • elementFormDefault="qualified" 

xmlns:xs specifies that the elements and data types used in xml schema document are defined in "http://www.w3.org/2001/XMLSchema" namespace and it should be prefixed with xs

targetNamespace specifies that the elements and attributes defined in your schema belongs to the "https://www.example.com" namespace.

xmlns specifies that the default namespace is https://www.example.com

elementFormDefault specifies that the value ‘qualified' indicating that an XML instance document must use qualified names for every element

 

Example:

     <?xml version="1.0"?>

     <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="https://www.mystore.com" xmlns="https://www. mystore.com" elementFormDefault="qualified">

                  <!--schema-->

     </xs:schema>

3.4.1  Association of XSD with XML

Approach 1:

  • You must declare the XMLSchema-instance namespace. It allows to link XSDs to XML files.
  • The xsi:noNamespaceSchemaLocation attribute defines the URL of your XSD 

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

             xsi:noNamespaceSchemaLocation="employee.xsd"

 Example:

        <?xml version="1.0"?>

        <Employee xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   xsi:noNamespaceSchemaLocation="employee.xsd">

            <EmpName>Ahmed</EmpName>

            <Dept>Sales</Dept>

       </ Employee >

 

Approach 2:

 This approach is most recommended for the following reasons:

  1.   Both the XML and the XSD file must contain a namespace declaration for your domain.
  2. The XML file must contain in addition:

    • A namespace declaration for XMLSchema-instance
    • A xsi:schemaLocation attribute that tells where to find the XSDs. This attribute can have as many "namespace-URL" pairs 

             xmlns="https://www.mystore.com"

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

            xsi:schemaLocation="https://www.mystore.com/xml/employee.xsd"

 Example:

            <?xml version="1.0"?>

           <Employee  xmlns="https://www.mystore.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="https://www. mystore.com/xml/ employee.xsd">

                 <EmpName>Ahmed</EmpName>

                 <Dept>Sales</Dept>

           </ Employee >

3.4.2 XML Schema Document Elements

XML Schema can define elements with different content.

 Syntax to declare element in schema

  <xs:element >   </xs:element>

 The following are the common attributes used in element declaration

 name- defines name of the element

type- defines the data type of the element

default- specifies the default value assigned to the element when no other value is assigned

fixed-specifies that no other value should be given other than the value assigned in fixed attribute

 

Example

   <xs:element name="title" type="xs:string" default="Mr"/>

   <xs:element name="company" type="xs:string" fixed="mystore"/>


3.4.2.1  Simple Elements


A simple element is an element that contains only simple data.

Example

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

         <xs:element name="dob" type="xs:date" />

Here are the common XML Schema primitive data types:


XML Schema Primitive Data Types
XML_Schema_Primitive_Data_Types_CKbMVT1.PNGXML Schema Primitive Data Types

XML Schema Primitive Data Types

3.4.2.2 Complex Elements

Complex elements defines elements with text, elements with elements, empty elements and elements with mixed content i.e., elements and text.

 Syntax to declare complex element

       <xs:element name="xyz">

          <xs:complexType>

            ……

          </xs:complexType>

        </xs:element>

 

Sub-elements are defined with different apparition rules. Sub-elements can be defined into a <xs:all/> markup in which the sub-elements must all exist and can appear in any order. Sub-elements can be defined into a <xs:sequence/> markup in which the sub-elements must appear in the same order. Sub-elements can be defined into a <xs:choice/> markup in which one and only one sub-element must appear.

Number of occurrences can be defined by using minOccurs and maxOccurs attributes of the <xs:element/> markup. By default, the minimum occurrence is 1 and the maximum occurrence is 1. To define an infinite number of occurrence, specify unbounded.

3.4.2.2.1  Complex Elements with Text

Syntax is

 <xs:element name="name of the element">

  <xs:complexType>

    <xs:simpleContent>

      <xs:extension base="datatype">

        …..

      </xs:extension>

    </xs:simpleContent>

  </xs:complexType>

</xs:element>

 

Example:

   <xs:element name="Salary">

    <xs:complexType>

      <xs:simpleContent>

        <xs:extension base="xs:double"/>

      </xs:simpleContent>

    </xs:complexType>

  </xs:element>

</xs:schema>


3.4.2.2.2  Complex Elements with Only Elements

Syntax is

<xs:element name="name of element">

  <xs:complexType>

    <xs:sequence> <!--required indicator-->

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

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

      ……

    </xs:sequence>

  </xs:complexType>

</xs:element>

 

Example:

 <xs:element name="EmpName">

  <xs:complexType>

    <xs:sequence>

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

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

       <xs:element name="contact " type="xs:string" minOccurs="1" maxOccurs="unbounded"/>

    </xs:sequence>

  </xs:complexType>

</xs:element>


 3.4.2.2.3 Complex Elements with Elements and Text

 Syntax is

<xs:element name="name of element">

  <xs:complexType mixed="true">

    <xs:sequence> <!--required indicator-->

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

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

      ……

    </xs:sequence>

  </xs:complexType>

</xs:element>

 

Example:

<xs:element name="EmpName">

  <xs:complexType mixed="true">

    <xs:sequence>

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

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

    </xs:sequence>

  </xs:complexType>

</xs:element>


3.4.2.3 Restrictions

Restrictions or facets used to specify the values acceptable for XML elements or attributes.

The following are all the schema restrictions and facets that can be used. 

XML Schema Restrictions
XML_Schema_Restrictions.PNGXML Schema Restrictions

XML Schema Restrictions

Examples:

1.       

<xs:element name="marks">

  <xs:simpleType>

    <xs:restriction base="xs:integer">

      <xs:minInclusive value="0"/>

      <xs:maxInclusive value="100"/>

    </xs:restriction>

  </xs:simpleType>

</xs:element>

 

2.      

  <xs:element name="gender">

  <xs:simpleType>

    <xs:restriction base="xs:string">

      <xs:enumeration value="male"/>

      <xs:enumeration value="female"/>

    </xs:restriction>

  </xs:simpleType>

</xs:element>

 

3.       

<xs:element name="phone_no">

  <xs:simpleType>

    <xs:restriction base="xs:string">

      <xs:length value="8"/>

    </xs:restriction>

  </xs:simpleType>

</xs:element>

 

4.       

<xs:element name="phone">

  <xs:simpleType>

    <xs:restriction base="xs:integer">

      <xs:pattern value="[7-9][0-9][0-9][0-9][0-9] [0-9][0-9][0-9]"/>

    </xs:restriction>

  </xs:simpleType>

</xs:element>

 

5.       

<xs:element name="email">

          <xs:simpleType>

            <xs:restriction base="xs:string">

              <xs:pattern value="([a-zA-Z])*@([a-zA-Z])*\.com"/>

            </xs:restriction>

          </xs:simpleType>

  </xs:element>


3.4.2.4 Any Element

<any> element is used to extend the elements in XML document which are not defined in XML schema.

 Example:

 Student.xsd

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

<xs:element name="student">

  <xs:complexType>

    <xs:sequence>

      <xs:element name="stu_no" type="xs:integer"/>

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

      <xs:any minOccurs="0"/>

    </xs:sequence>

  </xs:complexType>

</xs:element>

</xs:schema>


School.xsd

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

  <xs:element name="school_cgpa" type="xs:float"/>

</xs:schema>


Student.xml

<?xml version="1.0" encoding="utf-8"?>

<student xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  xsi:schemaLocation="www.studentinfo.com Student.xsd www.schoolinfo.com School.xsd">

  <stu_no>S100</stu_no>

  <stu_name title="Mr.">Ahmed </stu_name>

   <school_cgpa>2.3</school_cgpa>

</student>


3.4.3  XML Schema Document Attributes


The attributes of an element can be defined with the <xs:attribute/> markup.

 The syntax is:

       <xs:attribute name="name of the attribute" type="datatype"/>

 Example:

      <xs:attribute name="emp_id" type="xs:integer"/>


By default, attributes are optional. This can be changed with the use attribute. If its value is optional, the attribute can be left. If its value is required, the attribute must be present.

Example:

     <xs:attribute name="emp_id" type="xs:integer" use="required"/>

 

A default value can be defined with the default attribute. If the attribute is not present, the parsers will consider the attribute is present and its value is the default value.

Example:

    <xs:attribute name="emp_location" type="xs:string" default="HCT"/>

 

The attribute can be restricted to a constant value with the fixed attribute. As the fixed attribute also acts as a default value, you must not define a default attribute too.

 Example:

    <xs:attribute name="country" type="xs:string" fixed="Oman"/>


3.4.3.1 Attributes in Empty elements

<xs:element name ="CGPA">

          <xs:complexType>

            <xs:attribute name="level" type="xs:string"/>

          </xs:complexType>

 </xs:element>


3.4.3.2 Attributes in Elements with simple content

 <xs:element name="address">

          <xs:complexType>

            <xs:simpleContent>

              <xs:extension base="xs:string">

                <xs:attribute name="country" fixed="Oman"/>

              </xs:extension>

            </xs:simpleContent>

          </xs:complexType>

        </xs:element>


3.4.3.3 Attributes in Elements with element content

   <xs:element name="stu_name">

          <xs:complexType>

            <xs:sequence>

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

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

            </xs:sequence>

            <xs:attribute name="title" use="required">

              <xs:simpleType>

                <xs:restriction base="xs:string">

                  <xs:enumeration value="Mr."/>

                  <xs:enumeration value="Ms."/>

                </xs:restriction>

              </xs:simpleType>

            </xs:attribute>

          </xs:complexType>

    </xs:element>


3.4.3.4 anyAttribute

<anyAttribute> element is used to extend the attributes in XML document which are not defined in XML schema.

 

Example:

Student.xsd

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

<xs:element name="student">

  <xs:complexType>

    <xs:sequence>

      <xs:element name="stu_no" type="xs:integer"/>

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

       </xs:sequence>

      <xs:anyAttribute/>

  </xs:complexType>

</xs:element>

</xs:schema>


School.xsd

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

  <xs:attribute name="school_cgpa" type="xs:float"/>

</xs:schema>


Student.xml

<?xml version="1.0" encoding="utf-8"?>

<student xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"           xsi:schemaLocation=www.studentinfo.com Student.xsd www.schoolinfo.com School.xsd  school_cgpa =”2.3”>

  <stu_no>S100</stu_no>

  <stu_name title="Mr.">Ahmed </stu_name>

</student>



Exercise

Student.xml

<?xml version="1.0" encoding="utf-8"?>

<student xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  xsi:schemaLocation="www.xyz.com first.xsd">

  <stu_no>S100</stu_no>

  <stu_name title="Mr.">

    <first_name></first_name>

    <last_name></last_name>

  </stu_name>

  <dob day="1" month="12" year="2018"/>

  <address country="Oman">Al-Khuwair</address>

</student>


Student.xsd

<?xml version="1.0" encoding="utf-8"?>

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

  <xs:element name="student">

    <xs:complexType>

      <xs:sequence>

        <xs:element name="stu_no" type="xs:ID" maxOccurs="unbounded"/>

        <xs:element name="stu_name">

          <xs:complexType>

            <xs:sequence>

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

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

            </xs:sequence>

            <xs:attribute name="title" use="required">

              <xs:simpleType>

                <xs:restriction base="xs:string">

                  <xs:enumeration value="Mr."/>

                  <xs:enumeration value="Ms."/>

                </xs:restriction>

              </xs:simpleType>

            </xs:attribute>

          </xs:complexType>

        </xs:element>

        <xs:element name="dob">

          <xs:complexType>

             <xs:attribute name="day">

              <xs:simpleType>

                <xs:restriction base="xs:integer">

                  <xs:minInclusive value="1"/>

                  <xs:maxInclusive value="31"/>

                </xs:restriction>

              </xs:simpleType>

            </xs:attribute>

            <xs:attribute name="month">

              <xs:simpleType>

                <xs:restriction base="xs:integer">

                  <xs:minInclusive value="1"/>

                  <xs:maxInclusive value="12"/>

                </xs:restriction>

              </xs:simpleType>

            </xs:attribute>

            <xs:attribute name="year">

              <xs:simpleType>

                <xs:restriction base="xs:integer">

                  <xs:minInclusive value="1900"/>

                  <xs:maxInclusive value="2018"/>

                </xs:restriction>

              </xs:simpleType>

            </xs:attribute>

           </xs:complexType>

        </xs:element>

        <xs:element name="address">

          <xs:complexType>

            <xs:simpleContent>

              <xs:extension base="xs:string">

                <xs:attribute name="country" fixed="Oman"/>

              </xs:extension>

            </xs:simpleContent>

          </xs:complexType>

        </xs:element>

      </xs:sequence>

    </xs:complexType>

  </xs:element>

</xs:schema>



References


https://en.wikibooks.org/wiki/XML_Schema

http://edutechwiki.unige.ch/en/XML_Schema_tutorial_-_Basics

https://en.wikibooks.org/wiki/XML_-_Managing_Data_Exchange/Data_schemas

Return to top