Images

XML Schema - PART II


XML Schema
The definition of an XML document, which includes the XML tags and their interrelationships. Residing within the document itself, an XML schema may be used to verify the integrity of the content.
Various recommendations for an XML schema were submitted to theW3C, and a standard was approved in May 2001 that included the ability to define data by type (date, integer, etc.).
An XML schema is a description of a type of XML document, typically expressed in terms of constraints on the structure and content of documents of that type, above and beyond the basic syntax constraints imposed by XML itself.
Through a process called time-sharing, a large computer can handle interaction with hundreds of users simultaneously, giving each the perception of being the sole user.
An XML schema provides a view of the document type at a relatively high level of abstraction.


XS Complex Types
XS Complex Element
complex element is an XML element that contains other elements and/or 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
Note: Each of these elements may contain attributes as well!
Simple Types versus Complex Types
Simple types and complex types differ in this way: simple types cannot have element children or attributes; complex types may have element children and attributes.
Tracing the type hierarchy down the branch of simple types, we see that the first simple type is anySimpleType, which is also type that you could actually use. W3C XMLSchemas has 44 built-in simple types, each of which derives from anySimpleType, and all but three of which derive by restriction. Not a single one derives by extension.
To extend a simple type would mean to add element children or an attribute. This contradicts the definition of simple type in W3C Schemas and is thus prohibited.
Defining a Complex Type
complex type in an XML Schema is an XML element that may have other XMLelements within it. In HTML, an example of a complex type is the <body> tag. The body element can have within it many other elements, such as <p>, <table>, or <ol>.
The first complex type we find in the newsletter XML is NewsletterType. To define a complex type in a schema you use the complexType element:
lt;xsd:complexType name="NewsletterType">
Then you define the elements and attributes of that complex type. Because this type is the primary element of the newsletter, it contains all the different sub elements:
<xsd:complexType name="NewsletterType">
  <xsd:sequence>
    <xsd:element ref="comment" minOccurs="0"/>
    <xsd:element name="header" type="HeaderType"/>
    <xsd:element name="section1" type="SectionType"/>
    <xsd:element name="section2" type="SectionType"/>
    <xsd:element name="section3" type="SectionType"/>
    <xsd:element name="section4" type="SectionType"/>
  </xsd:sequence>
  <xsd:attribute name="volume" type="xsd:positiveInteger"/>
  <xsd:attribute name="number" type="xsd:positiveInteger"/>
</xsd:complexType> 
From this listing, we know that there may be one or more comments, a header field, and four sections. Plus there are two attributes of the NewsletterType: volume and number. We also know that the six sections of the newsletter will come in the following order:
1. comment (optional)
2. header
3. section1
4. section2
5. section3
6. section4
This declaration is a complex type that contains complex type definitions:
• HeaderType
• SectionType
But you can create a complex type that only includes elements with simple type definitions. For example, the HeaderType definition looks like this:
<xsd:complexType name="HeaderType">
  <xsd:sequence>
    <xsd:element name="long_title" type="xsd:string"/>
    <xsd:element name="filename" type="xsd:string"/>
    <xsd:element name="date" type="xsd:date"/>
    <xsd:element name="meta_title" type="xsd:string"/>
    <xsd:element name="meta_description" type="xsd:string"/>
    <xsd:element name="meta_keywords" type="xsd:string"/>
  </xsd:sequence>
</xsd:complexType> 
As with the NewsletterType, the HeaderType definition has a list of elements that will be found in a specific order. They can contain either character strings (xsd:string) or dates (xsd:date). They are:
• long_title
• filename
• date
• meta_title
• meta_description • meta_keywords
For most of the above definitions, I have declared a new type definition, but for the comment element, I simply refer to the existing element.
<xsd:element ref="comment" minOccurs="0"/>
This allows me to put the comment element in the document where it makes sense to put it. I could even include it in several different complex types. When it occurs above, I have set it to be optional with the minOccurs attribute set to 0. I could also set an upper limit on the number of comments in an element with the maxOccurs attribute.

XS Complex Types
XS Text only and Mixed Elements
A complex text-only element can contain text and attributes. This type contains only simple content (text and attributes), therefore we add a simpleContent element around the content. When using simple content, you must define an extension OR a restriction within the simpleContent element, like this:
<xs:element name="somename">
  <xs:complexType>
    <xs:simpleContent>
      <xs:restriction base="basetype">
        ....
        ....
      </xs:restriction>     
    </xs:simpleContent>
  </xs:complexType>
</xs:element>
Use the extension/restriction element to expand or to limit the base simple type for the element.
The following example declares a complexType, "department". The content is defined as a string value, and the " department dept-sub" element also contains an attribute named " dept-sub ":
<xs:element name="department">
       <xs:complexType>
        <xs:simpleContent>
         <xs:extension base="xs:string">
          <xs:attribute name="dept-sub" >
           <xs:simpleType>
            <xs:restriction base="xs:string">
            <xs:enumeration value="TECH"></xs:enumeration>
            <xs:enumeration value="ACNT"></xs:enumeration>
            <xs:enumeration value="CCNS"></xs:enumeration>
            &t;xs:enumeration value="MRKT"></xs:enumeration>
           </xs:restriction>
          </xs:simpleType>
         </xs:attribute>
        </xs:extension>
       </xs:simpleContent>
      </xs:complexType>
    </xs:element>
Click here to view this example.
We could also give the complexType element a name, and let the "department" element have a type attribute that refers to the name of the complexType (if you use this method, several elements can refer to the same complex type):
<xs:element name="department" type="deptType" />
        <xs:complexType name="deptType">
            <xs:simpleContent>
             <xs:extension base="xs:string">
             <xs:attribute name="dept-sub" >
             <xs:simpleType>
           <xs:restriction base="xs:string">
           <xs:enumeration value="JAVA"></xs:enumeration>
           <xs:enumeration value="PHPD"></xs:enumeration>
           <xs:enumeration value="ACNT"></xs:enumeration>
           <xs:enumeration value="CCNS"></xs:enumeration>
           <xs:enumeration value="MRKT"></xs:enumeration>
         </xs:restriction>
       </xs:simpleType>
      </xs:attribute>
     </xs:extension>
    </xs:simpleContent>
   </xs:complexType>
Click here to view this example.
Structure of the XSD document: xsdComplexTextOnly_alt.xsd
Structure of the  xsdComplexTextOnly_alt.xsd
Mixed Elements
A mixed complex type element can contain attributes, elements, and text.
Complex Types with Mixed Content
An XML element, "message” that contains both text and other elements:
 <message>
    Wish u a very Very Happy Friendship day    <banner>dasfa</banner>adf
        <highlights>10 year of our friendship</highlights>
          <msgContent>How r u?</msgContent> I miss u a lot
    </message>
The following schema declares the "message" element:
<xs:element name="message">
       <xs:complexType mixed="true" >
        <xs:sequence >
         <!--   <xs:any maxOccurs="unbounded" ></xs:any>-->
      <xs:element name="banner" type="xs:anyType"
	    maxOccurs="unbounded"></xs:element>
     <xs:element name="highlights" type="xs:anyType"
	  maxOccurs="unbounded"></xs:element>
     <xs:element name="msgContent" type="xs:string" 
	 maxOccurs="unbounded"></xs:element>  
   </xs:sequence>
  </xs:complexType>
 </xs:element>
Complete structure of the above Schema: xsmixedType.xsd
Complete structure of the above Schema: xsmixedType.xsd
To enable character data to appear between the child-elements of "message", the mixed attribute must be set to "true". The <xs:sequence> tag means that the elements defined (name, orderid and shipdate) must appear in that order inside a "message" element.
Complete source code of : xsmixedType.xsd
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:annotation>
    <xs:documentation source="http://www.ebizel.com">
    </xs:documentation>
</xs:annotation>
   <xs:element name="email">
       <xs:complexType>
           <xs:sequence>
  <xs:element ref="sender" maxOccurs="1" minOccurs="1"></xs:element>
   <xs:element ref="recipient" minOccurs="1" maxOccurs="5"></xs:element>
      <xs:element ref="subject"></xs:element>
         <xs:element ref="message"></xs:element>               
           </xs:sequence>
       </xs:complexType>
   </xs:element>
   <xs:element name="sender">
       <xs:complexType>
       <xs:sequence>
      <xs:element  ref="name"></xs:element>
   <xs:element name="email" type="emailID"></xs:element>
     </xs:sequence>
       </xs:complexType>
    </xs:element>
    <xs:element name="recipient">
       <xs:complexType>
         <xs:sequence>
       <xs:element ref="name"></xs:element>
    <xs:element name="email" type="emailID"></xs:element>
      </xs:sequence>
        </xs:complexType>
    </xs:element>
 <xs:element name="subject" type="xs:string"></xs:element>
    <xs:element name="message">
        <xs:complexType mixed="true" >
            <xs:sequence >
 
  <!--   <xs:any maxOccurs="unbounded" ></xs:any>-->
 <xs:element name="banner" type="xs:anyType" 
  maxOccurs="unbounded"></xs:element>
 <xs:element name="highlights" type="xs:anyType" 
 maxOccurs="unbounded"></xs:element>
 <xs:element name="msgContent" type="xs:string" 

maxOccurs="unbounded"></xs:element>                
    </xs:sequence>
   </xs:complexType>
  </xs:element>
 <xs:element name="name" type="NAME"></xs:element>
<xs:simpleType name="emailID">
        <xs:restriction base="xs:anyURI">
        <xs:pattern value=""></xs:pattern>
       </xs:restriction>
    </xs:simpleType>
   <xs:simpleType name="NAME" >
    <xs:restriction base="xs:string" >
   <xs:whiteSpace value="preserve"></xs:whiteSpace>
  <xs:maxLength value="25"></xs:maxLength>
 </xs:restriction>
</xs:simpleType>
 <xs:simpleType name="banner">
   <xs:restriction base="xs:string">
  
   </xs:restriction>
 </xs:simpleType>
</xs:schema>
Click here to view the example.
Source code of email.xml
<?xml version="1.0" encoding="UTF-8"?>
<email xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespaceSchemaLocation="xsmixedType.xsd">
    <sender>
        <name>fasdfasd dfasfasdfasd  </name>
        <email>ra.kumar@ebizel.com</email>
    </sender>
    <recipient>
        <name>sdfasf sdf </name>
        <email>someone@email.com</email>
    </recipient>
    <subject></subject>
    <message>
    Wish u a very Very Happy Friendship day    <banner>dasfa</banner>adf
        <highlights>10 year of our friendship</highlights>
          <msgContent>How r u?</msgContent> I miss u a lot
    </message>
</email>
Click here to view this example.
The alternate method…
We could also give the complexType element a name, and let the "message" element have a type attribute that refers to the name of the complexType (if you use this method, several elements can refer to the same complex type):
<xs:element name="message" type="msgType" ></xs:element>
<xs:complexType name="msgType"  mixed="true" >
        <xs:sequence >
  <!--   <xs:any maxOccurs="unbounded" ></xs:any>-->
<xs:element name="banner"
 type="xs:anyType"
  maxOccurs="unbounded"></xs:element>
<xs:element name="highlights" type="xs:anyType"
 maxOccurs="unbounded"></xs:element>
<xs:element name="msgContent" type="xs:string"
 maxOccurs="unbounded"></xs:element>         
  </xs:sequence>
 </xs:complexType>
Structure of xsmixedTypeAlt.xsd
Structure of xsmixedTypeAlt.xsd
Complete source code of xsmixedTypeAlt.xsd
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:annotation>
 <xs:documentation source="http://www.ebizel.com">
        
    </xs:documentation>
</xs:annotation>
   <xs:element name="email">
       <xs:complexType>
      <xs:sequence>
 <xs:element ref="sender" maxOccurs="1" minOccurs="1"></xs:element>
 <xs:element ref="recipient" minOccurs="1" maxOccurs="5"></xs:element>
       <xs:element ref="subject"></xs:element>
       <xs:element ref="message"></xs:element>               
     </xs:sequence>
    </xs:complexType>
   </xs:element>
    
    <xs:element name="sender">
       <xs:complexType>
           <xs:sequence>
         <xs:element  ref="name"></xs:element>
      <xs:element name="email" type="emailID"></xs:element>
        </xs:sequence>
       </xs:complexType>
    </xs:element>
    <xs:element name="recipient">
        <xs:complexType>
         <xs:sequence>
          <xs:element ref="name"></xs:element>
      <xs:element name="email" type="emailID"></xs:element>
        </xs:sequence>
        </xs:complexType>
    </xs:element>
    <xs:element name="subject" type="xs:string"></xs:element>
    <xs:element name="message" type="msgType" ></xs:element>
        
    <xs:element name="name" type="NAME"></xs:element>
   <xs:simpleType name="emailID">
      <xs:restriction base="xs:anyURI">
         <xs:pattern value=""></xs:pattern>
       </xs:restriction>
    </xs:simpleType>
<xs:simpleType name="NAME" >
    <xs:restriction base="xs:string" >
   <xs:whiteSpace value="preserve"></xs:whiteSpace>
    <xs:maxLength value="25"></xs:maxLength>
   </xs:restriction>
</xs:simpleType>
 <xs:simpleType name="banner">
 <xs:restriction base="xs:string">        
</xs:restriction>
 </xs:simpleType>
<xs:complexType name="msgType"  mixed="true" >
   <xs:sequence >
 <!--   <xs:any maxOccurs="unbounded" ></xs:any>-->
<xs:element name="banner" type="xs:anyType"
  maxOccurs="unbounded"></xs:element>
<xs:element name="highlights" type="xs:anyType"
 maxOccurs="unbounded"></xs:element>
 <xs:element name="msgContent" type="xs:string" 
 maxOccurs="unbounded"></xs:element>        
         
   </xs:sequence>
  </xs:complexType>
</xs:schema>

XS Complex Types
XS Indicators
With the help of XML Schema indicators we can control how the elements are to be used in the documents. Indicators can be useful to control the flow, occurrence, sequence of the element.
There are 7 different type of Indicators available, that can be categorized in three groups:
Indicators
1) Order indicators:
i) All
ii) Choice
iii) Sequence
2) Occurrence indicators:
i) maxOccurs
ii) minOccurs
3) Group indicators:
i) Group name
ii) attributeGroup name
Order Indicators
Order indicators are used to define the order of the elements. They are used to specify what will be the order of different elements.
1. Sequence
The <xs:sequence> indicator specifies that the child elements must appear in a specific order:
Source code of xsOrderSequence.xsd:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">


  <xs:element name="details">
    <xs:complexType>
      <xs:sequence >
      <xs:element name="name" type="xs:string"></xs:element>
       <xs:element name="address" type="xs:string"></xs:element>
       <xs:element name="phno" type="xs:string"></xs:element>
       </xs:sequence>
     </xs:complexType>
   </xs:element>
</xs:schema>
Click here to view the example.
Source of XML file based on above XML Schema
<?xml version="1.0" encoding="UTF-8"?>
<details xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespaceSchemaLocation="xsOrderSequence.xsd">
    <name>Ravish Sing</name>
    <address>Sector 44, Noida</address>
    <phno>99990</phno> 
</details>
Click here to view the example.
2. All Indicator
The <xs:all> indicator specifies that the child elements can appear in any order, and that each child element must occur only once:
<xs:complexType name="name">
            <xs:all>
           <xs:element name="fname" type="xs:string"></xs:element>
           <xs:element name="lname" type="xs:string"></xs:element>
            </xs:all>
</xs:complexType>
While using the name type element we can fname & lname elements in any order but each of them can appear only once.
Complete source code of the xsOrderAll.xsd
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
 
   <xs:complexType name="name">
   <xs:all>
     <xs:element name="fname" type="xs:string"></xs:element>
      <xs:element name="lname" type="xs:string"></xs:element>
        </xs:all>
        </xs:complexType>
 
    
    <xs:element name="details" >
     <xs:complexType>
     <xs:sequence>
     <xs:element name="empname" type="name"></xs:element>
    <xs:element name="address" type="xs:string"></xs:element>
   <xs:element name="mobile-no" >
     <xs:simpleType>
       <xs:restriction base="xs:string">
    <xs:pattern value="([+])([0-9]{12})"></xs:pattern>
     </xs:restriction>
    </xs:simpleType>
     </xs:element>
    </xs:sequence>
   </xs:complexType>
    </xs:element>
   
</xs:schema>
Click here to view this example. Source code of detailsAll.xml
<?xml version="1.0" encoding="UTF-8"?>
<details xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespaceSchemaLocation="xsOrderAll.xsd">
    <empname>
        <lname>Kumar</lname>
        <fname>Any</fname>
    </empname>
    <address> Sector 44, Noida</address>
    <mobile-no>+913294209999</mobile-no>
</details>
We can interchange the order of fname & lname
<?xml version="1.0" encoding="UTF-8"?>
<details xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespaceSchemaLocation="xsOrderAll.xsd">
    <empname>
         <fname>Any</fname>
        <lname>Kumar</lname>
      
    </empname>
    <address> Sector 44, Noida</address>
    <mobile-no>+913294209999</mobile-no>
</details>
.
Structure of the above schema
XS All
Click here to view the XML file
3. Choice Indicator
The <xs:choice> indicator specifies that either one child element or another can occur:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:annotation>
    <xs:documentation source="http://www.w3.org/">
XML Schema Tutorial by 
<a href="http://education.ebizel.com">eBIZ.com Education Team</a>.        
    </xs:documentation>
</xs:annotation>
    <xs:element name="ebiz">
        <xs:complexType>
      <xs:choice>
    <xs:element name="employee_details"></xs:element>
    <xs:element name="associate_details"></xs:element>
            </xs:choice>
        </xs:complexType>
    </xs:element>
    
</xs:schema>
Structure of the above schema
XML Schema Choice
Click here to view this file.
Occurrence Indicators
Occurrence indicators are used to define how often an element can occur.
Note: 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. maxOccurs Indicator
The <maxOccurs> indicator specifies the maximum number of times an element can occur:
<?xml version="1.0" encoding="UTF-8"?>
    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
     <xs:annotation>
      <xs:documentation source="http://www.w3.org/">
   XML Schema Tutorial by
 <a href="http://education.ebizel.com">eBIZ.com Education Team</a>.        
            </xs:documentation>
        </xs:annotation>
        <xs:element name="ebiz">
            <xs:complexType>
                <xs:choice>
  <xs:element name="employee_details" 
  maxOccurs="unbounded" ></xs:element>
  <xs:element name="associate_details"
   maxOccurs="unbounded"></xs:element>
                </xs:choice>
            </xs:complexType>
        </xs:element>
        
</xs:schema>
Tip: To allow an element to appear an unlimited number of times, use the maxOccurs="unbounded" statement
Click here to view the example.
Structure of the xsMaxoccur.xsd
Structure of the xsMaxoccur.xsd
Structure of the xsMaxoccur.xsd showing root
01
The example above indicates that either "employee_details” or“associate_details” element can occur a minimum of one time (the default value for minOccurs is 1) and a maximum unlimited times in the "ebiz" element.
minOccurs Indicator
The <minOccurs> indicator specifies the minimum number of times an element can occur:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
 
<xs:complexType name="name">
            <xs:all>
 <xs:element name="fname" 
 type="xs:string"  maxOccurs="1"></xs:element>
 <xs:element name="lname" type="xs:string" 
 minOccurs="0" maxOccurs="1"></xs:element>
            </xs:all>
        </xs:complexType>
 
    
    <xs:element name="details" >
   <xs:complexType>
  <xs:sequence>
 <xs:element name="empname" type="name"></xs:element>
  <xs:element name="address" type="xs:string"></xs:element>
  <xs:element name="mobile-no"  minOccurs="1" maxOccurs="2">
   <xs:simpleType>
      <xs:restriction base="xs:string">
        <xs:pattern value="([+])([0-9]{12})"></xs:pattern>
          </xs:restriction>
        </xs:simpleType>
      </xs:element>
    </xs:sequence>
   </xs:complexType>
 </xs:element>
   
</xs:schema>

The example above indicates that the "mobile-no" element can occur a minimum of 1 time and a maximum of 2 times in the "details" element.
Structure of the xsMin.xsd document
02
Click here to view the example.
Group Indicators
Group indicators are used to define related sets of elements.
1. Element Groups
Element groups are defined with the group declaration, like this:
<xs:group name="groupname"> ... </xs:group>
You must define an all, choice, or sequence element inside the group declaration. The following example defines a group named "personInfo", that defines a group of elements that must occur in an exact sequence:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:group name="personInfo">
    <xs:sequence>
  <xs:element name="fname" type="xs:string"
   maxOccurs="1" minOccurs="1"></xs:element>
  <xs:element name="lname" type="xs:string"
   minOccurs="0" maxOccurs="1"></xs:element>
 <xs:element type="xs:string" name="address"
  minOccurs="1" maxOccurs="1"></xs:element>
 </xs:sequence>
</xs:group>
    <xs:group name="empInfo" >
   <xs:sequence >
 <xs:element name="empID" type="xs:string"
  minOccurs="1" maxOccurs="1"></xs:element>
       <xs:element name="department">
      <xs:simpleType>
     <xs:restriction base="xs:string">
   <xs:enumeration value="TECH"></xs:enumeration>
   <xs:enumeration value="CCSS"></xs:enumeration>
   <xs:enumeration value="MRKT"></xs:enumeration>
   <xs:enumeration value="ADMN"></xs:enumeration>
   <xs:enumeration value="NA"></xs:enumeration>
      </xs:restriction>                    
    </xs:simpleType>                
 </xs:element>
        </xs:sequence>
    </xs:group>
  <xs:group name="associateInfo">
 <xs:sequence>
 <xs:element name="TID"></xs:element>
 <xs:element name="DID"></xs:element>
  </xs:sequence>
    </xs:group>
    <xs:element name="ebiz">
        <xs:complexType>
      <xs:choice>
 <xs:element ref="employee_details"
  maxOccurs="unbounded"></xs:element>    
 <xs:element ref="associate_details"
  maxOccurs="unbounded"></xs:element>
      </xs:choice>
     </xs:complexType>
    </xs:element>
    
    <xs:element name="employee_details">
        <xs:complexType>
      <xs:sequence>
     <xs:group ref="empInfo"></xs:group>
    <xs:group ref="personInfo"></xs:group>
  <xs:element name="dob" type="xs:date"></xs:element>
 </xs:sequence>
             
   </xs:complexType>
  </xs:element>
 <xs:element name="associate_details">
    <xs:complexType>
   <xs:sequence>
 <xs:group ref="associateInfo"></xs:group>
 <xs:group ref="personInfo"></xs:group>
 <xs:element name="doj" type="xs:date"></xs:element>
      </xs:sequence>
     </xs:complexType>
    </xs:element>
    
</xs:schema>
Structure of the above XSD document
03
Click here to view the example.
An XML file based on the above schema
<?xml version="1.0" encoding="UTF-8"?>
<ebiz xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespaceSchemaLocation="xsGroup.xsd">
    <employee_details>
        <empID>eBIZ0001</empID>
        <department>TECH</department>
        <fname>Any </fname>
        <lname>Kumar</lname>
        <address>Sector 66 noida</address>
        <dob>1984-09-26</dob>
    </employee_details>
    <employee_details>
        <empID>eBIZ0002</empID>
        <department>MRKT</department>
        <fname>Aman</fname>
        <lname>Kumar</lname>
        <address>sector 10 noida</address>
        <dob>1983-07-21</dob>
    </employee_details>
    
</ebiz>
Click here to view the example.

XS Complex Types
XS Element Substitution
Substitute
Let's say that we have users from two different countries: England and France. We would like the ability to let the user choose whether he or she would like to use theFrench element names or the English element names in the XML document.
To solve this problem, we could define a substitutionGroup in the XML schema. First, we declare a head element and then we declare the other elements which state that they are substitutable for the head element.
<xs:element name="name" type="xs:string"/>
<xs:element name="nom" substitutionGroup="name"/>
In the example above, the "name" element is the head element and the "nom" element is substitutable for "name".
Look at this XML schema:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">

<xs:element name="ebiz">
    <xs:complexType>
    <xs:sequence>
 <xs:element   ref="details" maxOccurs="unbounded" 
 minOccurs="1"></xs:element>
   </xs:sequence>
   </xs:complexType>
</xs:element>

<xs:complexType name="detailsInfo">
 <xs:sequence>
 <xs:element ref="name" ></xs:element>
 <xs:element ref="address" minOccurs="1"
  maxOccurs="1"></xs:element>
    </xs:sequence>
</xs:complexType>
    
<xs:element name="details" type="detailsInfo"></xs:element>
 <xs:element name="détails" 
 substitutionGroup="details"></xs:element>
    
    <xs:element name="name">
        <xs:complexType>
            <xs:sequence>
                <xs:sequence>
 <xs:element ref="first-Name" maxOccurs="1"
  minOccurs="1"></xs:element>
 <xs:element ref="last-Name" minOccurs="0" 
 maxOccurs="1"></xs:element>
                </xs:sequence>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
<xs:element name="nom" substitutionGroup="name"></xs:element>
    
<xs:element name="first-Name" type="xs:string" ></xs:element>
<xs:element name="prénom" substitutionGroup="first-Name"></xs:element>
    
<xs:element name="last-Name" type="xs:string"></xs:element>
<xs:element name="dernier-nom" 
substitutionGroup="last-Name"></xs:element>
    
<xs:element name="address" type="xs:string"></xs:element>
<xs:element name="adresse" substitutionGroup="address"></xs:element>
</xs:schema>
Structure of the above Schema
001
Click here to view the example.
Source code of XML file based on the above schema: XS-substitute.xml
<?xml version="1.0" encoding="UTF-8"?>
<ebiz xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespaceSchemaLocation="xsSubstitution.xsd">

    <!-- English Version of Details-->
    <details>
        <name>
  <first-Name>Dharmendra   </first-Name>
  <last-Name>Das</last-Name>
        </name>
 <address>D-210, Sector 101, Noida </address>
    </details>
    
    <!-- French Version of Details -->
    <!-- Version française des détails -->
    <détails>
        <nom>
       <prénom>Benoît</prénom>
     <dernier-nom></dernier-nom>
   </nom>
 <adresse>FRANCE MINIATURE, 25 ,route du Mesnil,
  78990 Elancourt</adresse>
      
  </détails>
</ebiz>
Click here to view this example.
Using substitutionGroup
The type of the substitutable elements must be the same as, or derived from, the type of the head element. If the type of the substitutable element is the same as the type of the head element you will not have to specify the type of the substitutable element.
Note that all elements in the substitutionGroup (the head element and the substitutable elements) must be declared as global elements, otherwise it will not work!
What are Global Elements?
Global elements are elements that are immediate children of the "schema" element! Local elements are elements nested within other elements.

XS Complex Types
XS <any>
The <any> element enables us to extend the XML document with elements not specified by the schema!
The <any> Element
The <any> element enables us to extend the XML document with elements not specified by the schema.
The following example is a fragment from an XML schema called "xs-any.xsd". It shows a declaration for the "details" element. By using the The &lt;any> and&lt;anyAttribute> elements are used to make EXTENSIBLE documents! They allow documents to contain additional elements that are not declared in the main XMLschema. element we can extend (after <address>) the content of "details" with any element:
Source code of :xs-any.xsd document
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="ebiz">
   <xs:complexType>
   <xs:sequence>
   <xs:element   ref="details" 
   maxOccurs="unbounded" minOccurs="1"></xs:element>
  </xs:sequence>
 </xs:complexType>
 </xs:element>
    
 <xs:element name="details" type="detailsInfo"></xs:element>
    
  <xs:complexType name="detailsInfo">
      <xs:sequence>
         <xs:element ref="name" ></xs:element>
        <xs:element ref="address" minOccurs="1"
 maxOccurs="1"></xs:element>
       <xs:any maxOccurs="1" minOccurs="0"></xs:any>    
     </xs:sequence>
    </xs:complexType>
   
    <xs:element name="name">
        <xs:complexType>
         <xs:sequence>
          <xs:sequence>
         <xs:element ref="first-Name" 
maxOccurs="1" minOccurs="1"></xs:element>
         <xs:element ref="last-Name" 
		 minOccurs="0" maxOccurs="1"></xs:element>
        </xs:sequence>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
  <xs:element name="nom" 
  substitutionGroup="name"></xs:element>
  <xs:element name="first-Name" 
  type="xs:string" ></xs:element>
 <xs:element name="prénom" 
 substitutionGroup="first-Name"></xs:element>
  <xs:element name="last-Name" 
  type="xs:string"></xs:element>
 <xs:element name="dernier-nom" 
 substitutionGroup="last-Name"></xs:element>
 <xs:element name="address" 
type="xs:string"></xs:element>
<xs:element name="adresse" 
substitutionGroup="address"></xs:element>
</xs:schema>
Structure of the above Schema
002
Click here to view the file.
Now we want to extend the "details” element with a "dob" element. In this case we can do so, even if the author of the schema above never declared any "dob" element.
Look at this schema file, called "xs-any1.xsd":
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" 
targetNamespace="http://www.ebizel.com" 
xmlns="http://www.ebizel.com" elementFormDefault="qualified">
<xs:element name="dob">
  <xs:simpleType>
   <xs:restriction base="xs:date">
            
    </xs:restriction>
  </xs:simpleType>
</xs:element>
</xs:schema>
Structure of the above Schema
003
Click here to view the file.
Source code for xs-any.xml
<?xml version="1.0" encoding="UTF-8"?>
<ebiz xmlns="http://www.ebizel.com" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:SchemaLocation="http://www.ebizel.com  xs-any.xsd 
    http://www.ebizel.com xs-any1.xsd">
    <details>
        <name>
            <first-Name></first-Name>
            <last-Name></last-Name>
        </name>
        <address></address>
        <dob>12-10-1984</dob>
    </details>
</ebiz>
The XML file above is valid because the schema "xs-any.xsd" allows us to extend the "details" element with an optional element after the "address" element.
The <any> and <anyAttribute> elements are used to make EXTENSIBLEdocuments! They allow documents to contain additional elements that are not declared in the main XML schema.

0 comments: