问题
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>
- 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