6

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.)

kjhughes
  • 106,133
  • 27
  • 181
  • 240
MengT
  • 1,207
  • 2
  • 14
  • 16
  • 4
    [Early working drafts of the schema spec](http://www.w3.org/TR/2000/WD-xmlschema-1-20000407/#declare-type) assumed an implicit `sequence` if you nested `element` declarations directly inside a `complexType`, but this was removed before the spec reached its final form in favour of always requiring an explicit `sequence`, `choice` or `all`. – Ian Roberts Oct 07 '13 at 12:35
  • OKay,I will pick the sequence :) – MengT Oct 07 '13 at 12:52

1 Answers1

10

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.

kjhughes
  • 106,133
  • 27
  • 181
  • 240
  • 1
    Your explanation and URL make everything clear, thank you! so it looks like XML Schema does not provide a compact way to declare single element complexType, where I must always put a single element into all | choice | sequence | group. – MengT Oct 07 '13 at 12:38
  • You can for all | choice | sequence | group add the restriction maxOccurs = "1", which will at least limit the amount of occurances to only one: – DEX Aug 05 '21 at 20:56
  • @DEX: `maxOccurs` already defaults to `1`. Besides, OP's question is about omitting `xs:sequence`, `xs:choice`, or `xs:all` altogether for the simple case where only single child element is allowed. (Answer as stated: Not permitted.) – kjhughes Aug 05 '21 at 22:48