问题
I have the following XML Schema
<xsd:complexType name="SimpleThing">
<xsd:choice maxOccurs="unbounded">
<xsd:group ref="simpleContent" />
</xsd:choice>
</xsd:complexType>
<xsd:complexType name="ExtendedThing">
<xsd:complexContent>
<xsd:extension base="SimpleThing">
<xsd:choice maxOccurs="unbounded">
<xsd:element name="qux" />
</xsd:choice>
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>
<xsd:group name="simpleContent">
<xsd:choice>
<xsd:element name="foo" />
<xsd:element name="bar" />
</xsd:choice>
</xsd:group>
<xsd:group name="extendedContent">
<xsd:choice>
<xsd:group ref="simpleContent" />
<xsd:element name="qux" />
</xsd:choice>
</xsd:group>
<xsd:element name="root" type="ExtendedThing" />
I would like the type ExtendedThing
to contain foo
, bar
and qux
in arbitrary order. But with this schema, qux
must come after foo
and bar
because a sequence, not a union of the choices is created from the choice of the supertype and the choice of the subtype.
The alternative, replacing the type definition for ExtendedThing
with
<xsd:complexType name="ExtendedThing">
<xsd:complexContent>
<xsd:extension base="SimpleThing">
<xsd:group ref="extendedContent" />
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>
doesn't work either, because the Unique Particle Attribution constraint is violated.
Is there any solution to this problem or is it impossible to create a type in XML Schema that is an extension of another type such that the content of the new type is a set of elements (in any order) that is a superset of the content of the supertype?
回答1:
This is not possible, for practical reasons. The content model of a super-type must always come in entirety before the content model of the sub-type. It helps to keep the implementation of validators fast.
来源:https://stackoverflow.com/questions/4216313/xml-schema-extend-xsdchoice-so-that-union-not-sequence-of-choices-is-created