开发者

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

开发者 https://www.devze.com 2023-01-08 12:19 出处:网络
I have following XSD code: <xsd:complexType name=\"questions\"> <xsd:sequence> <xsd:element name=\"location\" type=\"location\"/>

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开发者_JAVA百科, 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!


<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


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.


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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号