Difference between <xsd:all> and <xsd:sequence> in schema definition?

<xsd:all> specifies that the child elements can appear in any order.

<xsd:sequence> specifies child elements can only appear in the order mentioned.

Example for Sequence:

<xs:element name="compElement">
  <xs:complexType>
    <xs:sequence>
      <xs:element name="ele1" type="xs:string"/>
      <xs:element name="ele2" type="xs:string"/>
      <xs:element name="ele3" type="xs:string"/>
      <xs:element name="ele4" type="xs:string"/>
    </xs:sequence>
  </xs:complexType>
</xs:element>

If you create an XML from this xsd then, it will look something like this:

<compElement>
  <ele1>First</ele1>
  <ele2>Second</ele2>
  <ele3>Third</ele3>
  <ele4>Fourth</ele4>
</compElement>

Example for all:

<xs:element name="compElement">
  <xs:complexType>
    <xs:all>
      <xs:element name="ele1" type="xs:string"/>
      <xs:element name="ele2" type="xs:string"/>
      <xs:element name="ele3" type="xs:string"/>
      <xs:element name="ele4" type="xs:string"/>
    </xs:all>
  </xs:complexType>
</xs:element>

If you create an XML file from this xsd then it could look something like this:

<compElement>
  <ele2>Second</ele2>
  <ele1>First</ele1>
  <ele4>Fourth</ele4>
  <ele3>Third</ele3>
</compElement>

More info on xsd:all
More Info on xsd:sequence

Hope I answered your question.


Difference:

  • xsd:all - "child elements can appear in any order and each child element can occur zero or one time" (ie, maxOccurs can be 0 or 1)
  • xsd:sequence - "child elements must appear in a sequence. Each child element can occur from 0 to any number of times" (ie, maxOccurs can be 0 or any number or 'unbounded')

From the W3Schools tutorials here and here.


All Indicator

The <all> indicator specifies that the child elements can appear in any order, and that each child element must occur only once:

Sequence Indicator

The <sequence> indicator specifies that the child elements must appear in a specific order:

reference link