Search code examples
xmlxsdxsd-validationxml-validation

How to declare a complexType has only one child element?


When using XML Schema to declare that a complexType has just one child element, all the below three approaches achieve the goal:

<xs:complexType> <xs:sequence> <xs:element ref="somevalue"/> </xs:sequence> </xs:comlexType>
<xs:complexType> <xs:choice>   <xs:element ref="somevalue"/> </xs:choice>   </xs:comlexType>
<xs:complexType> <xs:all>      <xs:element ref="somevalue"/> </xs:all>      </xs:comlexType>

Apparently, the sequence, choice and all are not necessary to for a single element, because they should by used to indicate the order of multiple elements. Is there a more concise way to declare a complexType that has only one child element? (I.e. one that eliminates the use of sequence, all or choice, somehow.)


Solution

  • Just eliminating xs:sequence, xs:choice, or xs:all, is not an option:

      <xs:complexType name="cType">
        <xs:element name="e"/> 
      </xs:complexType>
    

    is not valid.

    See XML Representation of Complex Type Definitions where complexType's content model is defined as follows:

    (annotation?, (simpleContent | complexContent | ((group | all | choice | sequence)?, ((attribute | attributeGroup)*, anyAttribute?))))

    There is no provision for element being a direct child of complexType.