Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xml schema - define child elements 0-* in any order

Tags:

xml

xsd

I want to define a xml schema where the element Connectors have 0-* child elements. Either Sequence, Association or Message in any order and 0 to many times. I.e.

<Connectors>
    <Sequence />
    <Association />
    <Message />
    <Sequence />
    <Sequence />
    <Message />
    <Message />
    <Association />
</Connectors>

I tried to define the following schema but it seems like the order is fixed.

<xs:element name="Connectors">
    <xs:complexType>
        <xs:sequence>
            <xs:element ref="Association" minOccurs="0" maxOccurs="unbounded" />
            <xs:element ref="Message" minOccurs="0" maxOccurs="unbounded" />
            <xs:element ref="Sequence" minOccurs="0" maxOccurs="unbounded" />
        </xs:sequence>
    </xs:complexType>
</xs:element>
like image 851
Torbjörn Hansson Avatar asked Aug 31 '10 08:08

Torbjörn Hansson


People also ask

Do XML elements have to be in order?

In the most general sense, XML element order does not matter, unless otherwise specified by the appropriate schema.

Which schema tag allows to specify elements in any order in XML?

The all element specifies that the child elements can appear in any order and that each child element can occur zero or one time.

What minOccurs 0?

The minOccurs attribute specifies the minimum number of times that the element can occur. It can have a value of 0 or any positive integer. The maxOccurs attribute specifies the maximum number of times that the element can occur.

What is sequence in XML Schema?

Definition and Usage The sequence element specifies that the child elements must appear in a sequence. Each child element can occur from 0 to any number of times.


1 Answers

I solved it by setting to choice and setting minOccurs and maxOccurs attributes.

<xs:element name="Connectors">
    <xs:complexType>
        <xs:choice minOccurs="0" maxOccurs="unbounded">
            <xs:element ref="Association" />
            <xs:element ref="Message" />
            <xs:element ref="Sequence" />
        </xs:choice>
    </xs:complexType>
</xs:element>
like image 170
Torbjörn Hansson Avatar answered Oct 19 '22 15:10

Torbjörn Hansson