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.

Eg:

 <?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.w3schools.com"
xmlns="http://www.w3schools.com"
elementFormDefault="qualified">
...
...
</xs:schema>

xmlns:xs="http://www.w3.org/2001/XMLSchema" this specifies that the elements and datatypes are coming from "http://www.w3.org/2001/XMLSchema"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="http://www.w3schools.com" 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="http://www.w3schools.com"


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:

    xs:string
    xs:decimal
    xs:integer
    xs:boolean
    xs:date
    xs:time
   
 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.

 Attributes
 ------------

 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"/>
  </xs:restriction>
</xs:simpleType>
   
    </xs:simpleType>
    </sx:element>

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:sequence>
       <xs:element name="name" type="xs:String"/>
       <xs:element name="age"   type="xs:Integer"/> 
     </xs:sequence>
    </xs:complexType>
   
    <xs:complexType name="personalEmployeeInfo">
      <xs:complexContent>
       <xs:extension base="personalInfo"/>
         <xs:sequence>
           <xs:element name="address" type="xs:string"/>
         </xs:sequence>
      </xs:complexContent>
    </xs:complexType>

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">
  </xs:restrictions>
  </xs:element>
 
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:simpleContent>
     
      Eg:
     
      <xs:element name="shoe"type="shoeType"/>
     
      <xs:complexType name="shoeType">
      <xs:simpleContent>
      <xs:extension base="xs:integer">
         <xs:attribute name="brand" type="xs:string"/>
      </xs:simpleContent>
     
      </xs:complexType>
     
4.

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

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

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

         The Mixed attribute is made as True.
        
Indicators
------------

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.
    
     Eg:
    
        <xs:element name="employee" type="employeeType">
        </xs:element>
       
        <xs:complexType name="employeeType">
         <xs:all>
           <xs:element name="employeeName" type="xs:String"/>
            <xs:element name="employeeAge" type="xs:integer"/>
         </xs:all>
        </xs:complexType>
    
     b.choice
    
       Either of the type can come as child.
      
       Eg:
      
         <xs:element name="person">
           <xs:complexType>
             <xs:choice>
                <xs:element name="employee" type="employee"/>
                <xs:element name="member" type="member"/>
              </xs:choice>
           </xs:complexType>
         </xs:element>
        
     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