Can someone please give me some pointers on how my understanding og xsd:all element. I was under the impression that this allowed element to exist in any order or if minOccurs="0" is stipulated, allow elements to be optional or in any order. However I cant get the following XML to verify against the schema.
<xs:element name="Car"> <!-- global element. Not wrapped by type -->
<xs:complexType> <!-- embedded type -->
<xs:element name="engineType" type="engineType"/>
<xs:element name="partsValues" type="partsValue"/>
<xs:all minOccurs="0"> <!-- child elements can exist in any order -->
<xs:element name="engine" type="xs:integer"/> <!-- and can optionally exist -->
<xs:element name="exhaust" type="xs:integer"/>
<xs:element name="wheels" type="xs:integer"/>
Its complaining as I havent included exhaust. If I remove the minOccurs it complains as the order isnt correct. Im using XML Spy for validation.
Any help would be greatly appreciated.
Joined: Aug 19, 2010
<all minOccurs="0"> means combination of all elements can occur minimum zero times. In other words minOccurs value applies to group element <all> not individual element within <all></all> group. So in your case if you remove lines <engine>122</engine> and <wheels>33</wheels> XML validation succeed. If you change minOccurs="1"(Default) and do not provide <partsValues> group or provide empty <partsValues /> then it will complain about all 3 elements.
Hope this will help.
Joined: Oct 04, 2006
From my SCDJWS study notes:
Complex types can be declared containing the <all> element in the place of the <sequence> element. The <all> element cause the elements of the complex type to:
• Child elements of the complex type can appear in any order.
• Child elements always have a maxOccurs of 1 and a minOccurs of 0.
• Only single elements may occur in an <all> group.
No groupings like <sequence> or <all> can occur in an <all> group, however, custom types declared using either <simpleType> or <complexType> can occur in an <all> group.
I don't see why you want to include the minOccurs attribute on the <all> element - see above about minOccurs and maxOccurs of the child elements of the <all> element.
If you want the <partsValue> element to be optional, this is what you'd write in your schema:
If you use <all>, you cannot choose whether a child element of <all> is to be optional or not - they all are optional and can, at most, occur once.