| 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 |
|
|
 |
|
| 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 |
|
 |
|
| 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 showing root |
|
 |
|
| 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 |
|
 |
|
| 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 |
|
 |
|
| 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 |
|
 |
|
| 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 <any> and<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 |
|
 |
|
| 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 |
|
 |
|
| 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: