Difference between <xsd: all> and <xsd: sequence> in schema definition?
I am using xsd:all in a complex type. When I skip any required elements during validation, it displays all the elements. It does not display the exact missed item.
But if I use xsd:sequence , I can get the exact missing element.
Is there a difference between the two?
xsd:sequence : The XML element must be in the same order.
But xsd:all : the XML element can be in any order.
< xsd:all > indicates that children can be displayed in any order.
< xsd:sequence > indicates that children can only be displayed in that order.
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 XML from this xsd, it will be something like this:
<compElement> <ele1>First</ele1> <ele2>Second</ele2> <ele3>Third</ele3> <ele4>Fourth</ele4> </compElement> Example for everyone:
<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, it will look something like this:
<compElement> <ele2>Second</ele2> <ele1>First</ele1> <ele4>Fourth</ele4> <ele3>Third</ele3> </compElement> More information on xsd: all Additional information on xsd: sequence
I hope I answered your question.
Difference:
- xsd: all - "children can appear in any order, and each child can be zero or one time" (that is, maxOccurs can be 0 or 1)
- xsd: sequence - "children must appear in the sequence. Each child can occur from 0 to any number of times" (that is, maxOccurs can be 0 or any number or "unlimited"))
All indicators
The
<all>indicator indicates that children can be displayed in any order and that each child should appear only once:
Sequence indicator
The
<sequence>indicator indicates that children should be displayed in a specific order:
when we use in the tag, it indicates that all elements declared in this complexType should be displayed in the same order in the XML document. otherwise, you will receive an error message. since there is no need to specify the elements in the correct order.