How to make xsd element extend another

流过昼夜 提交于 2019-12-22 08:07:06

问题


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:

(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.

Thanks in advance.


回答1:


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>


来源:https://stackoverflow.com/questions/6672311/how-to-make-xsd-element-extend-another

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!