I’m trying to do some form of composition with restrictions in XML Schema.
I have a complexType that is reused in several other complexTypes. I want to specify that FieldA must be one value when PartialType is included in FullTypeA, and a different value when it’s included in FullTypeB.
<xs:complexType name="PartialType">
<xs:sequence>
<xs:element name="FieldA" type="xs:unsignedShort" />
<xs:element name="FieldB" type="xs:unsignedShort" />
</xs:sequence>
</xs:complexType>
<!-- FieldA must be 1 for FullTypeA-->
<xs:complexType name="FullTypeA">
<xs:sequence>
<xs:element ref="PartialType" />
<xs:element name="FieldC" type="xs:unsignedShort" />
</xs:sequence>
</xs:complexType>
<!-- FieldA must be 2 for FullTypeB-->
<xs:complexType name="FullTypeB">
<xs:sequence>
<xs:element ref="PartialType" />
<xs:element name="FieldD" type="xs:unsignedShort" />
</xs:sequence>
</xs:complexType>
How should I structure my XML Schema to accommodate this scenario?
I think Xsd doesn’t really allow this.
One possible way to achieve this would be using
xs:restrictionThe downside to this is obvious: You’ll have to declare all elements in your PartialType.
An alternative would be to use
xs:extension