45

I have following XSD code:

<xsd:complexType name="questions">
    <xsd:sequence>
        <xsd:element name="location" type="location"/>
        <xsd:element name="multipleChoiceInput" type="multipleChoiceInput" minOccurs="0" maxOccurs="unbounded"/>
        <xsd:element name="textInput" type="textInput" minOccurs="0" maxOccurs="unbounded"/>
        <xsd:element name="pictureInput" type="pictureInput" minOccurs="0"/>
    </xsd:sequence>
</xsd:complexType>

The problem here is: the elements location, multipleChoiceInput, etc. must appear in the same order they are declared. I don't want this to happen, I want that, in the validation process the sequence should not be relevant. How can I achieve this?

Another possibility I've tried has been:

<xsd:complexType name="questions">

        <xsd:choice maxOccurs="unbounded">   
            <xsd:element name="location" type="location"/>  
            <xsd:element name="multipleChoiceInput" type="multipleChoiceInput" minOccurs="0" maxOccurs="unbounded"/>
            <xsd:element name="textInput" type="textInput" minOccurs="0" maxOccurs="unbounded"/>
            <xsd:element name="pictureInput" type="pictureInput" minOccurs="0" maxOccurs="1"/>
        </xsd:choice>            

</xsd:complexType>

In this example, the sequence really does not matter anymore, and I can have so much elements as I want (what "all" would not allow me to do). But I still have the Problem with the min- and maxOccurs. In this example, I could have so many "pictureInput"s as possible, what is againt the constraint that I would like to have either 0 or 1.

Thanks a lot for helping!

jcborges
  • 1,064
  • 2
  • 10
  • 13

3 Answers3

56
<xsd:complexType name="questions">
    <xsd:all>
        <xsd:element name="location" type="location"/>
        <xsd:element name="multipleChoiceInput" type="multipleChoiceInput"/>
        <xsd:element name="textInput" type="textInput"/>
        <xsd:element name="pictureInput" type="pictureInput"/>
    </xsd:all>
</xsd:complexType>

NOTE: I have changed "sequence" to "all"

Sequence forces order (as defined). if order doesn't matter then all is used.

If there are chances of element occurence more than once then xsd:any can be used.

<xsd:complexType name="questions">
    <xsd:sequence>
        <xsd:any minOccurs="0"/>
    </xsd:sequence>
</xsd:complexType>

You can find details of xsd:any at following link:

https://www.w3schools.com/xml/schema_complex_any.asp

rkersh
  • 4,447
  • 2
  • 22
  • 31
YoK
  • 14,329
  • 4
  • 49
  • 67
  • 3
    Thanks for answering YoK, but "all" can not be used in my case, cause "all" requires the element to appear only ONCE (min- and maxOccurs can only accept the values 0 and 1). – jcborges Jul 24 '10 at 14:18
  • 1
    Then, maybe `` is your friend. – Tomalak Jul 24 '10 at 14:19
  • 1
    Ya in this case any needs to be used. Will also update answer. – YoK Jul 24 '10 at 14:42
  • Thanks again guys, but neither "ANY" nor "ALL" takes following in consideration: 1) I want to have, one, and only one, element "location" 2) I want to have 0 or 1 element "pictureInput"... Looking forward other suggestions. – jcborges Jul 24 '10 at 14:57
  • Did you verify that "ALL" doesn't work ? cause on following link it says: he all element provides an XML representation that describes an unordered set of element types. For each element type associated with an all element in an XML Schema Document, there must be a corresponding element in the corresponding XML instance. However, they may appear in the any order. In fact, there may be zero or many elements for each type depending upon the values of the minOccurs and maxOccurs attributes associated with the corresponding element type. http://www.xmlschemareference.com/allElement.html – YoK Jul 24 '10 at 15:25
  • Yes YoK, I've tried, and I got following error: Engine name: Xerces Severity: error Description: cos-all-limited.2: The {max occurs} of an element in an 'all' model group must be 0 or 1. The value '-1' for element 'multipleChoiceInput' is invalid. Start location: 39:31 (IN THIS CASE maxOccurs has been set to "unbounded") --------------------------------------------------------------------------------------------------------------------------------------------------------------------------- This error should be expected, cause "all" does not allow you to set maxOccurs bigger as 1. – jcborges Jul 24 '10 at 15:35
  • There is a [caveat](http://stackoverflow.com/a/12012599/603516) with unbounded elements in `all`. – Vadzim Sep 05 '16 at 13:53
23

I'm a little late to this discussion, but I had the same problem and found the solution:

<xsd:complexType name="questions">
    <xsd:choice maxOccurs="unbounded">
        <xsd:element name="location" type="location"/>
        <xsd:element name="multipleChoiceInput" type="multipleChoiceInput"/>
        <xsd:element name="textInput" type="textInput"/>
        <xsd:element name="pictureInput" type="pictureInput"/>
    </xsd:choice>
</xsd:complexType>

The key is to combine xs:choice with maxOccurs="unbounded". If you just use xs:all, you are allowed one of each, period.

edited to add: While xs:any will work, it won't limit your choices to the four elements itemized. It will allow anything, which pretty much defeats the purpose of a schema.

lime
  • 6,901
  • 4
  • 39
  • 50
CD Jorgensen
  • 1,361
  • 9
  • 8
  • 1
    For me this is the best approach to such a problem though it isn't perfect. In this case this does not respect the requirement to have 0 or 1 "pictureInput"s. You can add more than 1 and setting maxOccurs cannot prevent that (because the choice itself is unbound). – Ron Deijkers Feb 12 '15 at 15:30
1

Also very late to the party here, but would using <xsd:all> in conjunction with minOccurs and maxOccurs not work?:

<xsd:complexType name="questions">
    <xsd:all>
        <xsd:element name="location" type="location" minOccurs="0" maxOccurs="1"/>
        <xsd:element name="multipleChoiceInput" type="multipleChoiceInput" minOccurs="0" maxOccurs="1"/>
        <xsd:element name="textInput" type="textInput" minOccurs="0" maxOccurs="1"/>
        <xsd:element name="pictureInput" type="pictureInput" minOccurs="0" maxOccurs="1"/>
    </xsd:all>
</xsd:complexType>
gaelicyoda
  • 71
  • 1
  • 8
  • 1
    No, because inside of any you can't define maxOccurs greater than 1 – sotix Jun 11 '15 at 06:47
  • @sotix: I don't understand your comment: The answer uses `all` and not `any` and also there _are_ no `maxOccurs > 1`. So wouldn't it actually work using `all`? – Marcus Mangelsdorf May 07 '18 at 19:14
  • @MarcusMangelsdorf it's been a while, so I can only re-read. Judging by the accepted answer `all` can not contain `maxOccurs > 1`, which was part of the original question. So I guess I mixed up `all` and `any` in the previous comment. – sotix May 09 '18 at 05:38
  • 1
    You're absolutely right, it won't work for the original question and its a typo for sure. But @gaelicyoda's code sample is still correct and works (it was exactly what I needed), although the `maxOccurs="1"` are superfluous, since [the element defaults to `minOccurs="1"` and `maxOccurs="1"`](https://www.w3schools.com/xml/el_all.asp). – Marcus Mangelsdorf May 11 '18 at 09:21