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

6

95

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.

Banky answered 19/4, 2013 at 9:30 Comment(4)
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?Brigid
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 logicKroeger
Is there an answer on this question already? I would also like to know if I always have to use <sequence> to get the exact missed object.Macdonald
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.Ctenoid
S
155

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

Sontich answered 19/4, 2013 at 9:46 Comment(2)
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 element3Banky
You can specify " minOccurs" attribute of "all " element as zero. For more details please refer w3schools.com/schema/el_all.aspSontich
E
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.

Ecuador answered 19/4, 2013 at 9:32 Comment(7)
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.Brigid
w3schools is not affiliated with W3C, so their webpages are not W3C docs.Plasmodium
@Ecuador Can you cite where <xs:all> constrains the number of times a child element can occur? I cannot find evidence of this in the W3C spec.Crossopterygian
@LukePuplett See section 3.8.2 of this spec. In there, in the first row of the table, the minOccurs and maxOccurs are restricted to <0;1>.Ecuador
@Ecuador 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.Crossopterygian
This is quite confusing. You should add that the default occurrence for each element in the sequence is exactly 1.Textbook
"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.Spicate
C
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.

Ctenoid answered 1/2, 2018 at 12:5 Comment(2)
How does that answer the question?Cymry
The question appears to ask about the difference between sequence and all, but a more thorough reading reveals the author already knows this, but is puzzled by the output from his validator. That's how.Ctenoid
P
5

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.

Pawl answered 11/4, 2020 at 13:37 Comment(0)
F
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

Foreboding answered 19/4, 2013 at 9:34 Comment(1)
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.Brigid
B
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.

Barstow answered 9/2, 2017 at 2:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.