1

I have a requirement to produce an XSD. Under the root element there can be 0, 1 or multiple occurrences of any of 7 different elements, and these elements can occur in any order.

I can't use sequence, since the elements are not necessarily in a predefined order. This would be a valid schema, but it imposes too severe a restriction:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="data">
<xs:complexType>
<xs:sequence>

I can't use all, since it doesn't allow maxOccurs to be unbounded, so this is an invalid schema:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="data">
<xs:complexType>
<xs:all>
<xs:element name="address" minOccurs="0" maxOccurs="unbounded">

I have a feeling I've come up against another limitation of XSD, but I just thought I'd ask as I am new to XML Schemas.

4

1 回答 1

1

Use a choice block with a maxOccurs="1" on each element. This will ensure there is at least one of either a, b, or c but no more than one from each.

<xs:choice minOccurs="1" maxOccurs="unbounded">
  <xs:element name="a" maxOccurs="1"/>
  <xs:element name="b" maxOccurs="1"/>
  <xs:element name="c" maxOccurs="1"/>
</xs:choice>

All of the following are valid under this schema:

<root>
    <a/>
</root>

<root>
    <a/>
    <b/>
</root>

<root>
    <b/>
    <a/>
</root>

<root>
    <c/>
    <a/>
</root>

<root>
    <a/>
    <c/>
    <b/>
</root>
于 2013-08-16T14:48:19.907 回答