XML validation with XSD: how to avoid caring about the sequence of the elements?
Asked Answered
D

3

47

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!

Diablerie answered 24/7, 2010 at 13:23 Comment(0)
R
57
<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

Renfroe answered 24/7, 2010 at 14:9 Comment(7)
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).Diablerie
Then, maybe <xs:any> is your friend.Upsweep
Ya in this case any needs to be used. Will also update answer.Renfroe
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.Diablerie
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. xmlschemareference.com/allElement.htmlRenfroe
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.Diablerie
There is a caveat with unbounded elements in all.Archicarp
J
25

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.

Jointly answered 23/12, 2011 at 16:56 Comment(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).Rebuttal
B
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>
Bazooka answered 26/5, 2015 at 11:11 Comment(4)
No, because inside of any you can't define maxOccurs greater than 1Referendum
@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?Helga
@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.Referendum
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 <all> element defaults to minOccurs="1" and maxOccurs="1".Helga

© 2022 - 2024 — McMap. All rights reserved.