XSD和多态 [英] XSD and polymorphism
问题描述
我有点在第一次问错这个问题时重复了这个问题.
I am kinda repeating this question bit the 1st time it was asked incorrectly.
我有这个:
<xsd:complexType name="A">
<xsd:sequence>
<xsd:element name="options" type="options"/>
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="B">
<xsd:complexContent>
<xsd:element name="options" type="ex_options"/>
</xsd:complexContent>
</xsd:complexType>
<xsd:complexType name="options">
<xsd:sequence>
...some options
</xsd:sequence>
</xsd:element>
<xsd:complexType name="ex_options">
<xsd:complexContent>
<xsd:extension base="options">
<xsd:sequence>
...some more options
</xsd:sequence>
</xsd:extension>
</xsd:complexContent>
</xsd:element>
所以基本上我有一个A类,带有一个内部选项类 类B继承自类A,我希望B.options继承自A.options,这样,当我们进行网络服务时,我们只需要传递a;当我们调用getOptions时,它将返回正确的对象B.options.目前,以xsd表示的方式出现一个错误,说多个具有不同类型名称选项的元素出现在模型组中.错误属于B型.
So basically I have class A with an inner class of options Class B inherits from class A and I want B.options to inherit from A.options so that when we do webservices we only need to pass a and when we call getOptions it will return the right object B.options. Currently with the way the xsd stands I get an error saying multiple elements with name options with different types appear in the model group. The errors is in the B type.
推荐答案
只需坚持使用B类型的元素,然后使用,然后使用适当的xsi:type
属性值按如下所述装饰实例文档元素.
Just stick with elements of type B and then use then decorate your instance document elements as described below with the appropriate xsi:type
attribute value.
<xsd:complexType name="B">
<xsd:complexContent>
<xsd:element name="options" type="ex_options"/>
</xsd:complexContent>
</xsd:complexType>
<xsd:complexType name="options">
<xsd:sequence>
...some options
</xsd:sequence>
</xsd:element>
<xsd:complexType name="ex_options">
<xsd:complexContent>
<xsd:extension base="options">
<xsd:sequence>
...some more options
</xsd:sequence>
</xsd:extension>
</xsd:complexContent>
</xsd:element>
然后将其实例元素装饰"为任意一个
and then "decorate" your instance element as either
<options xsi:type="ex_options"> ... (this will work)
或
<options xsi:type="options"> ... (I think you can do this as long as the base xsi:type is not abstract)
如果事实证明您无法使用基本的xsi:type
装饰,
那么您总是可以通过创建一个空的基本类型来作弊",然后
通过精心的构造来扩展,以达到您想要的两种格式.
If it turns out that you can't decorate with the base xsi:type
,
then you can always "cheat" by creating an empty base type and then
extending by careful construction to arrive at your two desired formats.
请参见这篇文章以进行详细说明&链接.
See this post for more elaboration & links.
这篇关于XSD和多态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!