我有三个不同的 XML 元素,它们有一些共同的标签。
例如:人有姓名、年龄、性别
然后我有经理、员工,它们将共享人员拥有的三个字段以及经理、员工特定字段,如 managerNo、employeeNo 等。
我可以在 xsd 中写一些像这样的东西吗:
<强>1。声明 Person 元素
<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>
- 使用上面的 Person 声明并扩展 Manager 元素:
实际上,我试图按照 Java(面向对象)继承来模仿我的模式定义,例如:
public class Person {
String name;
int age;
// getters and setters for above variables go here
}
然后做:
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
}
我想在 xsd 中模仿这种 Java 继承概念,这样我就可以声明一个基本元素,然后扩展该基本元素,这样其他子元素也可以继承基本元素的属性。
最佳答案
只需使用:
<xs:extension base="AddressType">
在您的 Manager/Employye 模式定义中
<xs:complexType name="Manager">
<xs:complexContent>
<xs:extension base="Person">
<xs:sequence>
<!-- Properties -->
</xs:sequence>
</xs:extension>
</xs:complexContent>
</xs:complexType>
关于xml - 如何使 xsd 元素扩展另一个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6672311/