问题描述
我有一组特定元素封装在一个 complexType 中,如果用户想要以某种方式开展业务,则需要这些元素(让我们称这种方式为 x):
I have a set of certain elements encapsulated in a complexType that are required if a user wants to do business in a certain way (lets call this way x):
<xs:element name="us">
<xs:complexType>
<xs:sequence>
<xs:element name="usAttribute1" type="xs:string"/>
<xs:element name="commonAttribute1" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
以及封装在不同 complexType 中的特定元素集,如果用户希望以不同的方式开展业务,则需要这些元素(我们称之为 y):
and a certain set of elements encapsulated in a different complexType that are required if the user wants to do business in a different way (lets call this way y):
<xs:element name="uk">
<xs:complexType>
<xs:sequence>
<xs:element name="ukAttribute1" type="xs:string"/>
<xs:element name="commonAttribute1" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
两个集合之间存在元素重叠,因此某些元素对两个集合来说都是共同的.假设用户希望以 x 和 y 两种方式开展业务.我们如何将这两组元素组合起来,使元素不重复?
There is an overlap of elements between both sets such that some elements are common to both sets. Let's say that the user would like to do business in both way x and way y. How do we combine the two sets of elements so that there isn't any duplication of elements?
推荐答案
你想要的基本上是 XML Schema 中的多重继承".
What you want is basically "multiple inheritance" in XML Schema.
不能直接(你只从一种类型派生),但有选项:
It is not possible directly (you derive from one type only), but there are options:
- 您可以使用模型组(
xs:group
、xs:attributeGroup
),您可以引用其中的几个(归功于@jasso).从我读到的在规范中,你应该能够将commonAttribute1
元素包含两次,只要包含的内容或多或少相同即可. - 您可能希望使用聚合而不是继承.这通常是一种更好、更简单的方法.
- You can use model groups (
xs:group
,xs:attributeGroup
), you can reference several of them (credit goes to @jasso). From what I read in the specification, you should be able to include thecommonAttribute1
element two times as long as the inclusion is more or less identical. - You may want to use aggregation instead of inheritance. It is often a better and easier approach.
这篇关于从多个 complexType 创建一个 complexType的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!