94

I am using xsd:all in a complex type. When I miss any mandatory elements while validating it will show all the elements. It will not display the exact missed element.

But if I am use xsd:sequence I can get the exact missed element.

Is there any difference between these two?

xsd:sequence: XML element must be in same order.

But xsd:all: XML element may be any order.

Stefan van den Akker
  • 6,661
  • 7
  • 48
  • 63
user1679378
  • 1,370
  • 3
  • 17
  • 23
  • Fundamentally, the difference between these two is what you've already indicated in your question. However, the constraints associated with the use of these two compositors and the implications of those in XSD authoring depend on which spec you're referring to: XSD 1.0 or XSD 1.1? – Petru Gardea Apr 19 '13 at 16:51
  • 3
    Looks like ur question is about why sequence and all write error in different way when it find missing element. I think it is about parser logic – Nasir Oct 27 '15 at 05:36
  • Is there an answer on this question already? I would also like to know if I always have to use to get the exact missed object. – GertV Nov 28 '16 at 13:01
  • 1
    You are getting unsatisfactory answers because your title is misleading: The replies are correctly answering the question in the title, about the *meaning* of "all" vs "sequence". From your comments it seems that your real problem is a difference in how your validator reports *validation errors* for the two. This is not the same thing, and in any case impossible to answer without knowing which validator you are using. – Joachim Lous Feb 01 '18 at 12:14

6 Answers6

154

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

Alexis Wilke
  • 19,179
  • 10
  • 84
  • 156
Madhusudan Joshi
  • 4,438
  • 3
  • 26
  • 42
  • Hi Joshi ..Thanks for ur comments.But my question is when I validate the xml against xsd while using xsd:all it will not show the exact missed element.For example element1 expected ..Instead it will show all elements element1,element2,element3, EXPECTED but I gave minOccurs=0 for element2 and element3 – user1679378 Apr 26 '13 at 09:02
  • 2
    You can specify " minOccurs" attribute of "all " element as zero. For more details please refer http://www.w3schools.com/schema/el_all.asp – Madhusudan Joshi Apr 26 '13 at 09:38
24

Difference:

  • xsd:all - Child elements can appear in any order. By default, each child element occurs exactly once. The allowed number of occurrences of child elements can be modified by the minOccurs and maxOccurs attributes.
  • xsd:sequence - Child elements must appear in the defined order. By default, each child element occurs exactly once. The allowed number of occurrences of child elements can be modified by the minOccurs and maxOccurs attributes.

From the W3Schools tutorials here and here.

sikr_
  • 369
  • 1
  • 7
  • 19
kamituel
  • 34,606
  • 6
  • 81
  • 98
  • 4
    You should qualify the constraints on the cardinality of the particle as being specific to XSD 1.0 - otherwise this is incorrect for XSD 1.1. – Petru Gardea Apr 19 '13 at 16:45
  • 10
    w3schools is not affiliated with W3C, so their webpages are not W3C docs. – Ben Companjen May 13 '14 at 13:08
  • @kamituel Can you cite where `` constrains the number of times a child element can occur? I cannot find evidence of this in the W3C spec. – Luke Puplett Aug 21 '14 at 11:57
  • @LukePuplett See [section 3.8.2](http://www.w3.org/TR/xmlschema11-1/#declare-contentModel) of [this spec](http://www.w3.org/TR/xmlschema11-1/). In there, in the first row of the table, the `minOccurs` and `maxOccurs` are restricted to <0;1>. – kamituel Aug 21 '14 at 12:10
  • 2
    @kamituel Actually, that applies to the all element itself within its container in the schema. The doc you cite is 1.1 which does not have the constraint. Anyway, I did find the contraint, its in "human English" just as you pasted in your answer, a few lines above in the 1.0 doc. – Luke Puplett Aug 22 '14 at 00:15
  • This is quite confusing. You should add that the default occurrence for each element in the sequence is exactly 1. – SWIIWII Oct 04 '17 at 01:24
  • 3
    "child element can occur zero or one time" is actually incorrect! Default value is 1 for minOccurs, meaning it MUST be present, and this applies to both all and sequence. – PKCS12 Jul 05 '19 at 09:10
6

The schema merely defines what constitutes a compliant document.

How non-compliance is reported is entirely up to the validator. There is nothing stopping a validator from reporting exactly which fields are missing, but apparently the one you use does not in this case.

Whether that is a bug or by design you would have to discuss with the provider of the validator.

Joachim Lous
  • 1,316
  • 1
  • 14
  • 22
4

SIMPLE XML EXAMPLE:

<school>
  <firstname>John</firstname>
  <lastname>Smith</lastname>
</school>

XSD OF ABOVE XML(Explained):

<xs:element name="school">
  <xs:complexType>
    <xs:sequence>
      <xs:element name="firstname" type="xs:string"/>
      <xs:element name="lastname" type="xs:string"/>
    </xs:sequence>
  </xs:complexType>
</xs:element>

Here:

xs:element : Defines an element.

xs:all : Denotes child elements can appear in any order.

xs:sequence : Denotes child elements only appear in the order mentioned.

xs:complexType : Denotes it contains other elements.

xs:simpleType : Denotes they do not contain other elements.

type: string, decimal, integer, boolean, date, time,

  • In simple words, xsd is another way to represent and validate XML data with the specific type.
  • With the help of extra attributes, we can perform multiple operations.

  • Performing any task on xsd is simpler than xml.

shubham1js
  • 242
  • 3
  • 7
2

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

rewritten
  • 16,280
  • 2
  • 47
  • 50
NPKR
  • 5,368
  • 4
  • 31
  • 48
  • 2
    You should qualify the constraints on the cardinality of the particle as being specific to XSD 1.0 - otherwise this is incorrect for XSD 1.1. – Petru Gardea Apr 19 '13 at 16:44
0

when we use under tag, it indicates all the elements that are declared in that complexType MUST appear in same order in XML document. otherwise, you will get an error. for there is no need to specify elements in proper order.