Why does xs:choice allow multiple different xs:element in the resulting xml?
Asked Answered
G

1

5

I know about this related question and I understand that my question contradicts that answer, but the following XML file validates perfectly against the following XMLSchema

my XML data:

<?xml version="1.0" encoding="utf-8"?>
<myElements>
    <el1>bla</el1>
    <el1>bla</el1>
    <el1>blabla</el1>
    <el2>bgt</el2>
    <el2>pel</el2>
    <el3>sdf</el3>
</myElements>

my XMLSchema:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="myElements">
        <xs:complexType>
            <xs:choice minOccurs="0" maxOccurs="unbounded">
                <xs:element name="el1" />
                <xs:element name="el2" />
                <xs:element name="el3" />
                <xs:element name="el4" />
            </xs:choice>
        </xs:complexType>
    </xs:element>
</xs:schema>

W3schools and other sources say that:

XML Schema choice element allows only one of the elements contained in the declaration to be present within the containing element.

In my opinion this means that my XML data should accept exactly only one of these:

  • 0 to any el1
  • 0 to any el2
  • 0 to any el3
  • 0 to any el4

But if you try to validate my xml data to my xmlschema, it is valid, and I do not understand why.

Do I completely misunderstand the documentation?

Is the validator I am using to lax and non-standard? If yes, what would be a good validator and why would anyone use a choice in a situation like I described? (Yes, I did encounter this)

Gossamer answered 19/9, 2016 at 16:54 Comment(3)
Your schema is saying, for 0 to n times, allow a single element of these choices... In other words, since the choice occurrence is boundless, you can have one element from the choice any number of times.Humperdinck
so, my XML data shouldn't be valid, but it is. You can check it on freeformatter.com/xml-validator-xsd.htmlGossamer
No, it is valid; you have 6 instances of your choice of one element. Your schema says, separately: 1) This choice can occur any number of times, and 2) This choice consists of 1 element from these options. So you're making a single choice, multiple times.Humperdinck
A
9

Why does xs:choice allow multiple different xs:element in the resulting xml?

Because you added maxOccurs="unbounded" to xs:choice.

Your understanding of what xs:choice/@maxOccurs="unbounded" means requires adjustment.

It does not mean that you first make the choice, and then repeat that chosen element per the occurrence constraints on xs:choice.

It does mean that you can make the choice as many times as is allowed by the occurrence constraints on xs:choice. Choices are independent, so child elements of xs:choice may be interleaved when xs:choice/@maxOccurs is greater than 1.

If you want to express the constraint that a single choice may be made and the chosen element may be repeated consecutively, then place maxOccurs="unbounded" on the children of xs:choice rather than on xs:choice itself.

Almatadema answered 19/9, 2016 at 17:24 Comment(1)
I finally got it, a maxOccurs on xs:choice practically tells you how many children (or choices) are allowed, it doesn't matter the order, or how many of each they are, unless there are other restrictions in the elements themselves. Thank you!Gossamer

© 2022 - 2024 — McMap. All rights reserved.