KarelHusa
KarelHusa

Reputation: 2258

XSD validation of an element defined as an empty choice

I have encountered the XSD element definition with an empty choice:

   <xs:element name="Data">
    <xs:complexType>
     <xs:choice>
     </xs:choice>
    </xs:complexType>
   </xs:element>

When I get the element instance:

<Data/>

within an XML document, some of the parsers (SoapUI, Oracle SOA 12c) evaluate it as an error: Expected element(s) in element Data@http://www.namespace.com/ensc).

While others (Eclipse tools) evaluate the XML as Schema valid.

I wonder which evaluation result is correct.

Upvotes: 1

Views: 242

Answers (1)

kjhughes
kjhughes

Reputation: 111726

An empty xs:choice is allowed in XSD:

<choice
  id = ID
  maxOccurs = (nonNegativeInteger | unbounded)  : 1
  minOccurs = nonNegativeInteger : 1
  {any attributes with non-schema namespace . . .}>
  Content: (annotation?, (element | group | choice | sequence | any)*)
</choice>

Notice that Content: may consist of an optional annotation followed by zero or more of (element | group | choice | sequence | any). Your XSD is therefore fine, and an XML document consisting of just Data would be valid against it.

Your XSD could be simplified to just

<xs:element name="Data">
  <xs:complexType/>
</xs:element>

Note, however, that it could not be further simplified to

<xs:element name="Data"/>

As that would actually allow Data to have any attributes and content model.

Upvotes: 2

Related Questions