Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make xsd element extend another

Tags:

wsdl

xml

xsd

I have three different XML elements that have some common tags.

For e.g: Person has name, age, sex

Then I have Manager, Employee that would share the three fields the Person has plus Manager, Employee specific fields like managerNo, employeeNo etc.

Can I write something in xsd that would be like this:

1. Declare Person element

<xsd:element name="Person">
        <xsd:annotation>
            <xsd:documentation>Person Request</xsd:documentation>
        </xsd:annotation>
        <xsd:complexType>
            <xsd:sequence>              
                <xsd:element name="personname" type="xsd:string" minOccurs="1" maxOccurs="1" /> 
                <xsd:element name="age" type="xsd:integer" minOccurs="1" maxOccurs="1" />   
            </xsd:sequence>
        </xsd:complexType>
</xsd:element>
  1. Use the above Person declaration and extend the Manager element:

<xsd:element name="Manager" extends="tns:Person"> (just idea of what I am looking for)

In effect, I am trying to mimic my schema definition as per Java (object oriented) inheritance like:

public class Person {
   String name;
   int age;

   // getters and setters for above variables go here
}

then do:

public class Manager extends Person {
   int managerNo;
   String departmentName;
}

public class Employee extends Person {
   int employeeNo;
   String designation;

 // getters/setters and other code goes here
}

I want to mimic this Java inheritance concept in the xsd such that I can declare one base element, and just extend that base element such that other child elements also inherit the properties of base element.

like image 848
user841717 Avatar asked Jul 12 '11 23:07

user841717


1 Answers

Simply use:

<xs:extension base="AddressType"> 

in your Manager/Employye schema definition

<xs:complexType name="Manager">
    <xs:complexContent>
        <xs:extension base="Person"> 
            <xs:sequence>
                <!-- Properties -->
            </xs:sequence>
        </xs:extension>
    </xs:complexContent>
</xs:complexType>
like image 147
Mrchief Avatar answered Nov 19 '22 01:11

Mrchief