Saturday, April 27, 2013

XML Schema Demistyfied

DTD's have limited namespace support so they are not used for webservice schema.

Xml Schema is used to implement webservices.

The Elements of XML Schema

<xs:schema> the schema element is the root element.It can contain more attributes.


 <?xml version="1.0"?>
<xs:schema xmlns:xs=""

xmlns:xs="" this specifies that the elements and datatypes are coming from ""name space and also these elements and datatypes must have a prefix xs.

This need not be xmlns:xs alone it can be xmlns:xsd or any name.

targetNamespace="" indicates that the elements defined by this schema comes from the namespace enclosed in quotes.

a default namespace can be defined using xmlns="".
Eg: xmlns=""

elementFormDefault="qualified" defines that the elements used in schema should be namespace qualified.

XSD Simple Elements

Xml Schema contains simple elements.Simple elements can be anyhting like text,boolean,data etc.

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

XML Schema has a lot of built-in data types. The most common types are:

 There are 2 more attributes that can come for an xs:element

 <xs:element name="color" type="xs:string" default="red">

 here default will be automatically applied.

  <xs:element name="color" type="xs:string" fixed="red">
 here by default a fixed value is being passed to the variable.


 Simple elements do not have attributes to define attributes we need to add an attribute element.

 <xs:attribute name="lang" type="xs:string">

 can use default and final. The attribute can be defines as required or optional.

  <xs:attribute name="lang" type="xs:string" use="required"/>
  Restrictions On Value
 <xs:element name="car" type="carType"/>

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

Note: Restrictions always come under the simpleType Tag

 There are 2 types of elements Simple Type and Complex type.See example above for simple type.

Complex Elements

Complex elements are XMl Elements that contains other elements/Attributes.

There are four kinds of complex elements:

    empty elements
    elements that contain only other elements
    elements that contain only text
    elements that contain both other elements and text
    Eg: CE.1
    <xs:element name="employee" type="personalEmployeeInfo"/>
    <xs:complexType name="personalInfo">
       <xs:element name="name" type="xs:String"/>
       <xs:element name="age"   type="xs:Integer"/> 
    <xs:complexType name="personalEmployeeInfo">
       <xs:extension base="personalInfo"/>
           <xs:element name="address" type="xs:string"/>

1. Empty Complex Element can have only attributes no contents

  <xs:element name="address">
  <xs:restrictions base="xs:boolean">
  <xs:attribute name="isUs" type="xs:boolean">
2. Complex type which contains only elements
    Reffer Eg: CE.1
3. This contains only simple content(text/attributes) and so we wrap it with
      <xs:element name="shoe"type="shoeType"/>
      <xs:complexType name="shoeType">
      <xs:extension base="xs:integer">
         <xs:attribute name="brand" type="xs:string"/>

        <letter>Dear Mr.<name>John Smith</name>.
              Your order <orderid>1032</orderid>
              will be shipped on <shipdate>2001-07-13</shipdate>.

     Schema Ex:
       <xs:element name="letter" type="lettertype"/>

        <xs:complexType name="lettertype" mixed="true">
           <xs:element name="name" type="xs:string"/>
           <xs:element name="orderid" type="xs:positiveInteger"/>
           <xs:element name="shipdate" type="xs:date"/>

         The Mixed attribute is made as True.

There are seven Indicators.

 For all "Order" and "Group" indicators (any, all, choice, sequence, group name, and group reference) the default value for maxOccurs and minOccurs is 1.
 1. Order Indicators

     a. All
     This indicates that all the child elements can occur in any order  and can occur only once.
        <xs:element name="employee" type="employeeType">
        <xs:complexType name="employeeType">
           <xs:element name="employeeName" type="xs:String"/>
            <xs:element name="employeeAge" type="xs:integer"/>
       Either of the type can come as child.
         <xs:element name="person">
                <xs:element name="employee" type="employee"/>
                <xs:element name="member" type="member"/>
     c.   Sequence
        The child elements must appear in a specific order:
            Reffer Eg: CE.1
    To allow an element to appear an unlimited number of times, use the maxOccurs="unbounded" statement:

No comments:

Post a Comment