Learn XML Schema by Example: 2 Polymorphism

In the previous blog, Learn XML Schema by Example: 1. Complex type, I introduced the very simple idea of ComplexType in Schema. In this blog, I am going to introduce idea of polymorphism which is one of the very important concept in Object orientation.

clip_image001

As shown in the above figure, I need Address to be replaced by AUAddress or USAddress in the instance xml file. All the Address, USAddress and AUAddress are complex types in the XML schema. These types are defined in the address.xsd file as shown in the following code.

<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns="http://www.ojitha.org/address" targetNamespace="http://www.ojitha.org/address" xmlns:xs="http://www.w3.org/2001/XMLSchema"
  elementFormDefault="qualified" attributeFormDefault="unqualified">

      <xs:complexType name="AUAddress">
      <xs:complexContent>
        <xs:extension base="Address">
         <xs:sequence>
           <xs:element name="state" maxOccurs="1">
           <xs:simpleType>
            <xs:restriction base="xs:string">
                <xs:enumeration value="ACT"/>
                <xs:enumeration value="VIC"/>
            </xs:restriction>
           </xs:simpleType>
            </xs:element>
         </xs:sequence>
          <xs:attribute name="country" type="xs:NMTOKEN"  fixed="AU" use="required"/>
          </xs:extension>
     </xs:complexContent>
     </xs:complexType>

    <xs:complexType name="USAddress">
      <xs:complexContent>
        <xs:extension base="Address">
         <xs:sequence>
           <xs:element name="state" maxOccurs="1">
           <xs:simpleType>
            <xs:restriction base="xs:string">
                <xs:enumeration value="AK"/>
                <xs:enumeration value="AL"/>
            </xs:restriction>
           </xs:simpleType>
            </xs:element>
         </xs:sequence>
          <xs:attribute name="country" type="xs:NMTOKEN"  fixed="US" use="required"/>
          </xs:extension>
     </xs:complexContent>
     </xs:complexType>

     <xs:complexType name="Address">
       <xs:sequence>
        <xs:element name="street" type="xs:string" maxOccurs="1"/>
        <xs:element name="city" type="xs:string"/>
       </xs:sequence>
     </xs:complexType>
</xs:schema>

 

Following figure shows these three types how they are related.

image

Let us create a another schema which represent the person, who is having address which can be either AUAddress or USAddress.

<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns="http://www.ojitha.org" targetNamespace="http://www.ojitha.org" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:addr="http://www.ojitha.org/address" elementFormDefault="qualified" attributeFormDefault="qualified">

      <xs:import namespace="http://www.ojitha.org/address"
         schemaLocation="file:///C:/XmlPad%20Projects/first/address.xsd"/>
         
      <xs:annotation>
        <xs:documentation>
          This is testing schema for a purchase order.
        </xs:documentation>
      </xs:annotation>
     <xs:element name="process">
      <xs:complexType>
       <xs:sequence>
           <xs:element name="person" type="personType"/>
       </xs:sequence>
      </xs:complexType>
     </xs:element>
     
     <xs:complexType name="personType">
      <xs:sequence>
        <xs:element name="name">
           <xs:complexType>
             <xs:simpleContent>
               <xs:extension base="xs:string">
                 <xs:attribute name="sex" use="required">
                   <xs:simpleType>
                     <xs:restriction base="xs:string">
                       <xs:enumeration value="M"/>
                       <xs:enumeration value="F"/>
                     </xs:restriction>
                   </xs:simpleType>
                 </xs:attribute>
               </xs:extension>
            </xs:simpleContent>
           </xs:complexType>
        </xs:element>
        <xs:element name="age">
         <xs:simpleType>
           <xs:restriction base="xs:positiveInteger">
             <xs:minInclusive value="12"/>
             <xs:maxExclusive value="100"/>
           </xs:restriction>
         </xs:simpleType>
        </xs:element>
             <xs:element name="address" type="addr:Address"/>
      </xs:sequence>
     </xs:complexType>
 </xs:schema>

The <address> element can be either AUAddress or USAddress in the instance XML files as shown in the following,

<?xml version="1.0" encoding="utf-8"?>
<process xmlns="http://www.ojitha.org" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:addr="http://www.ojitha.org/address" xsi:schemaLocation="http://www.ojitha.org file:///C:/XmlPad%20Projects/first/po.xsd">

<person>
    <name sex="M">Ojitha</name>
    <age>40</age>
    <address xsi:type="addr:AUAddress" country="AU">
          <addr:street>Ten</addr:street>
          <addr:city>bonython</addr:city>
          <addr:state>ACT</addr:state>
    </address>
  </person>
</process>

As well you can have USAddress also.

<?xml version="1.0" encoding="utf-8"?>
<process xmlns="http://www.ojitha.org" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:addr="http://www.ojitha.org/address" xsi:schemaLocation="http://www.ojitha.org file:///C:/XmlPad%20Projects/first/po.xsd">

<person>
    <name sex="M">Ojitha</name>
    <age>40</age>
    <address xsi:type="addr:USAddress" country="US">
          <addr:street>Ten</addr:street>
          <addr:city>bonython</addr:city>
          <addr:state>AL</addr:state>
    </address>
  </person>
</process>

Hope this will help. If there is other way please comment.

Comments

Popular posts from this blog

How To: GitHub projects in Spring Tool Suite

Spring 3 Part 7: Spring with Databases

Parse the namespace based XML using Python