This is a generalized example of what I am opposed to.
I created derived types in my schema and want to create an element that is an unlimited list (sequence) that has a restriction where only two of the three derived types are allowed.
To say this from the top level, "I have events in which there can only be two types of events in one situation."
This is how I defined my events and the subsequent sequence holder. (It all works and works).
An abstract element is a complex type called "Event Base" and has a common Name attribute:
<xs:complexType name="EventBase"> <xs:annotation><xs:documentation>***Abstract Event***</xs:documentation></xs:annotation> <xs:attribute name="Name"/> </xs:complexType>
Then I have three events stemming from the abstract, as shown below.
<xs:complexType name="DerivedEvent1"> <xs:complexContent> <xs:extension base="EventBase"> <xs:attribute name="Alpha" type="xs:string"/> </xs:extension> </xs:complexContent> </xs:complexType> <xs:complexType name="DerivedEvent2"> <xs:complexContent> <xs:extension base="EventBase"> <xs:attribute name="Beta"/> </xs:extension> </xs:complexContent> </xs:complexType> <xs:complexType name="DerivedEvent3"> <xs:complexContent> <xs:extension base="EventBase"> <xs:attribute name="Gamma"/> </xs:extension> </xs:complexContent> </xs:complexType>
To simplify a complex object for storing derivative events, I create a specific โgeneralโ event derived from an abstract complex
<xs:element name="Event" type="EventBase"> <xs:annotation><xs:documentation>A generic event derived from abstract.</xs:documentation></xs:annotation> </xs:element>
Then I want to be able to hold events, so I create a new complex object to hold the โcommonโ event shown above, but actually hold the derivative events to the potential consumer.
<xs:complexType name="EventsCollectionType"> <xs:annotation><xs:documentation>Holds derived events</xs:documentation></xs:annotation> <xs:sequence> <xs:element ref="Event" maxOccurs="unbounded"/> </xs:sequence> </xs:complexType>
Finally, I create an element derived from a collection type that will contain the actual events:
<xs:element name="Events"><xs:annotation><xs:documentation>Concrete holder of events.</xs:documentation></xs:annotation> <xs:complexType> <xs:sequence> <xs:element ref="Event" maxOccurs="unbounded"/> </xs:sequence> </xs:complexType> </xs:element>
The resulting xml is as follows:
<?xml version="1.0" encoding="UTF-8"?> <Events xsi:noNamespaceSchemaLocation="file:///C:/StackOverflow.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <Event xsi:type="DerivedEvent1" Name="D1" Alpha="Content1"/> <Event xsi:type="DerivedEvent3" Name="D1" Gamma="Content3"/> </Events>
So the question is, how can I create a final * * * event element that will only contain specific xsi: typed elements?
Thus, in the case where a restriction has been established, only valid types 1 and 3 (as indicated above); but if he had a derived type 2, that would be wrong.
I created a public GIST ( Limit or Limit on xsi: type )