Reputation: 3736
I need to create an XSD schema of a list of items:
<xs:element name="Items">
<xs:complexType>
<xs:sequence>
<xs:element name="Item" minOccurs="0" maxOccurs="unbounded" type="Item" />
</xs:sequence>
</xs:complexType>
</xs:element>
Each item has a name and an id, but the order of these elements is unknown and can vary for each item. So I use <xs:all>
like this:
<xs:complexType name="Item">
<xs:all>
<xs:element name="Name" type="xs:string"/>
<xs:element name="ID" type="xs:integer"/>
</xs:all>
</xs:complexType>
This works. However, I have 3 kinds of items. Each item has an additional element:
None of the elements have attributes, and the items don't need to be extended further.
I tried using extension but, you can't use <xs:all>
in the parent and then extend.
Then I tried making one superItem, where an item can have a choice of either fish, vegetables or fruits but you can't use choice inside <xs:all>
.
How can I use extension with <xs:all>
, or can I create a workaround to not have the fixed order of elements that sequence requires? In the real code, my item and subtype of items have many more elements than the ones shown above, so doing a choice between all possible permutations is not feasible.
Upvotes: 2
Views: 1900
Reputation: 111541
You'll need XML Schema 1.1 to add elements via extension to an xsd:all
content model. If you can live with an imposed element ordering, you can use xsd:sequence
instead with either XSD 1.0 or 1.1.
See XML Schema Part 1: Structures Second Edition, section 2.2.1.3 Complex Type Definition:
Note: This specification allows only appending, and not other kinds of extensions. This decision simplifies application processing required to cast instances from derived to base type. Future versions may allow more kinds of extension, requiring more complex transformations to effect casting.
See W3C XML Schema Definition Language (XSD) 1.1 Part 1: Structures, section 2.2.1.3 Complex Type Definition:
Note: For the most part, this specification allows only appending, and not other kinds of extensions. This decision simplifies application processing required to cast instances from the derived type to the base type. One special case allows the extension of all-groups in ways that do not guarantee that the new material occurs only at the end of the content. Another special case is extension via Open Contents in interleave mode.
Upvotes: 1