xml Xsd 和继承
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/204852/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Xsd and inheritance
提问by arinte
I have an xsd like this
我有一个这样的 xsd
<xsd:complexType name="A">
<xsd:complexContent>
<xsd:sequence>
<xsd:element name="options">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="Day">
...
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexContent>
</xsd:complexType>
<xsd:complexType name="B">
<xsd:complexContent>
<xsd:extension base="A">
...What would go here...
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>
So basically I want class A to have a sequence of options (Day, Week for example) then I want B to inherit from A and have all of A's options and an additional 2 or 3 options like hours, seconds.
所以基本上我希望 A 类有一系列选项(例如,天、周)然后我希望 B 从 A 继承并拥有 A 的所有选项和额外的 2 或 3 个选项,如小时、秒。
回答by Richard Nienaber
Here's the schema I came up with:
这是我想出的架构:
<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="inheritance"
targetNamespace="http://test.com"
elementFormDefault="qualified"
xmlns="http://www.w3.org/2001/XMLSchema"
xmlns:test="http://test.com"
>
<xs:element name="Time">
<xs:complexType>
<xs:sequence>
<xs:element name="First" type="test:A" />
<xs:element name="Second" type="test:B" />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:complexType name="shortOptions">
<xs:sequence>
<xs:element name="Day" />
</xs:sequence>
</xs:complexType>
<xs:complexType name="longOptions">
<xs:complexContent>
<xs:extension base="test:shortOptions">
<xs:sequence>
<xs:element name="Week" />
</xs:sequence>
</xs:extension>
</xs:complexContent>
</xs:complexType>
<xs:complexType name="A">
<xs:sequence>
<xs:element name="options" type="test:shortOptions" />
</xs:sequence>
</xs:complexType>
<xs:complexType name="B">
<xs:sequence>
<xs:element name="options" type="test:longOptions" />
</xs:sequence>
</xs:complexType>
</xs:schema>
Which seems to fit this xml:
这似乎适合这个xml:
<?xml version="1.0" encoding="utf-8" ?>
<Time xmlns="http://test.com">
<First>
<options>
<Day>Today</Day>
</options>
</First>
<Second>
<options>
<Day>Tomorrow</Day>
<Week>This Week</Week>
</options>
</Second>
</Time>
回答by csgero
Simply add an <xsd:sequence>with the required elements:
只需添加<xsd:sequence>所需元素:
<xsd:complexType name="B">
<xsd:complexContent>
<xsd:extension base="A">
<xsd:sequence>
<xsd:element name="Hours">
...
</xsd:element>
</xsd:sequence>
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>
回答by nedruod
You will need to define options as a complex type of it's own, then use extension on that to create a new options complex type and use substitution instead of extension.
您需要将选项定义为它自己的复杂类型,然后在其上使用扩展来创建新的选项复杂类型并使用替换而不是扩展。
回答by tunaranch
You'll need to create a type for options, which contains hours etc, and then add options instead of hours in csgero's answer.
您需要为选项创建一个类型,其中包含小时等,然后在 csgero 的答案中添加选项而不是小时。

