Sunday, September 18, 2011

XSD (XML Schema Definition)


XSD (XML Schema Definition)

XSD basically defines the way an XML file should be defined and structured. It serves as a design tool; a framework on which XML implementations can be built.
Since XSD is an xml document, it is easy to learn and implement. One of the biggest advantages of XSD lies in the fact that it supports inheritance which means re-usability.

XSD Base Data Types

A base data type can be used to create user defined types. For example a “string” base type can be used to define a “fullName” type data in the following manner:
<xs:element name="fullName" type="xs:string" />

XSD provides a variety of such data types. Some of the more common are: string, int, dates, Boolean, decimal, double and so on.

XSD Facets

Facets provide restrictions to the way base data types are used. One example of using such restrictions is as follows:
<xs:element name="password">
  <xs:simpleType>
    <xs:restriction base="xs:string">
      <xs:length value="8"/>
    </xs:restriction>
  </xs:simpleType>
</xs:element>

XSD Schema Element Types

XSD elements can be of two types: simple and complex.
a)      Simple: it allows user defined type to be created from given “base data type”. An example is:
<xs:element name="fullName" type="xs:string" />

b)      Complex: when an element is supposed to contain child elements, then it has to be defined as a complex type. An example is:
<xs:element name="complexElement">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="testChild1" type="xs:string" />
        <xs:element name="testChild2" type="xs:int" />
        <xs:element name="testChild3" type="userDefinedType" />
      </xs:sequence>
    </xs:complexType>
  </xs:element>

Annotations (Comments)

XSD provides commenting feature with “annotation” element.  Unlike XML comments (<!—comment text -->), the element is a part of the schema component. It consists of two child elements: documentation and appinfo.
a)      documentation: provides information about the purpose of the source code
b)      appinfo:  provides information about the application to the user

Case Study

Consider a scenario that XYZ institute is a human resource training company. It requires information from the trainees to give them a certificate after successfully completing any sort of training.

Following is an example of XML schema for the above requirement:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="XMLSchema1"
    targetNamespace="http://tempuri.org/XMLSchema1.xsd"
    elementFormDefault="qualified"
    xmlns="http://tempuri.org/XMLSchema1.xsd"
    xmlns:mstns="http://tempuri.org/XMLSchema1.xsd"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
> 
  <xs:element name="trainee">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="regNo" type="xs:short"/>
        <xs:element name="fullName" type="xs:string" />
        <xs:element name="fatherName" type="xs:string" />
        <xs:element name="permanentAddress" type="xs:string" />
        <xs:element name="citizenNo" type="xs:string" />
        <xs:element name="passportNo" type="xs:string" />
        <xs:element name="creditHrs" type="xs:short" />
        <xs:element name="system" type="xs:string" />
        <xs:element name="fromDate" type="xs:date" />
        <xs:element name="toDate" type="xs:date" />
        <xs:element name="courses" type="courseList" />
        <xs:element name="receiveMail" type="udf1"  />
      </xs:sequence>
    </xs:complexType>
  </xs:element>

  <xs:annotation>
    <xs:documentation>This element provides information for end user</xs:documentation>
    <xs:appinfo>This element provides information to the developer about the code</xs:appinfo>
  </xs:annotation>

  <xs:annotation>
    <xs: documentation>
      The "courseList" simpleType is a list of strings that defines the courses element.
      "courseList" is an example of global scoped simple type element.

      The "udf1" simpleType is used to store the confirmation from trainees if they want
      to participate in further training programs.

    </xs: documentation>
  </xs:annotation>

  <xs:simpleType name="courseList">
    <xs:list itemType="xs:string" />
  </xs:simpleType>

  <xs:simpleType name="udf1">
    <xs:restriction base="xs:string">
      <xs:enumeration value="Y" />
      <xs:enumeration value="N" />
    </xs:restriction>
  </xs:simpleType>

</xs:schema>

And, following is one of the XML files containing the actual data:

<?xml version="1.0" encoding="utf-8"?>
<trainee xmlns="http://tempuri.org/XMLSchema1.xsd">
  <!-- This xml file contains the information on a trainee. -->
  <regNo>5915</regNo>
  <fullName>Madam Bahadur</fullName>
  <fatherName>Mohan Bahadur</fatherName>
  <permanentAddress>Kausaltar,Bhaktapur</permanentAddress>
  <citizenNo>48569/Bhaktapur</citizenNo>
  <passportNo>12597A</passportNo>
  <creditHrs>20</creditHrs>
  <system>Foreign Labor System</system>
  <fromDate>2010-10-01</fromDate>
  <toDate>2010-12-02</toDate>
  <courses>korean cooking refrigeration </courses>
  <receiveMail>Y</receiveMail>
</trainee>

I just shared the small things I learned about XSD. Hopefully this will be useful. For detailed information on XSD,  please go to w3c website at http://www.w3c.org/ or msdn website.

References:

·         http://www.15seconds.com/issue/031209.htm
·         http://msdn.microsoft.com/en-us/library/ms256235.aspx
·         http://www.w3schools.com/schema/schema_intro.asp


No comments:

Post a Comment