You can't use xs:all
in XSD 1.0 because although it allows elements in any order, it only allows one or zero of each.
But the schema you provided does not require any child elements to be present, which means that an item
like this:
<item></item>
would still be valid (since all the attributes are also declared as optional
). Is that the case?
If so, you don't need <xs:all>
and can replace the <xs:sequence>
inside the <xs:element name="item">
declaration for an optional and unbounded <xs:choice>
:
<xs:element name="item" maxOccurs="unbounded" minOccurs="1">
<xs:complexType>
<xs:choice minOccurs="0" maxOccurs="unbounded">
...
</xs:choice>
<xs:attribute type="xs:short" name="id" use="optional"/>
...
</xs:complexType>
</xs:element>
This will validate empty <item>
elements, <item>
elements containing child elements in any order, any quantity (including zero). It will validate this:
<item>
<tag id="752" title="пираты"/>
<tag id="894" title="любовь"/>
<category id="71" title="Фильмы"/>
<tag id="1164" title="флот"/>
<country id="515" title="США"/>
<tag id="1789" title="море"/>
<director id="27449" title="Гор Вербински"/>
<tag id="1790" title="сокровища"/>
<tag id="1811" title="нечисть"/>
<genre id="702" title="приключения"/>
<tag id="2292" title="похищение"/>
<year id="227" title="2003"/>
<genre id="674" title="художественное"/>
<genre id="690" title="боевик"/>
<tariffs>
<tariff id="2" type-id="1" type-alias="subscription" price="179" duration="30"/>
<tariff id="6" type-id="2" type-alias="purchase" price="79" duration="30"/>
</tariffs>
<genre id="5938" title="фэнтези"/>
</item>
And this:
<item>
<tag id="1789" title="море"/>
<director id="27449" title="Гор Вербински"/>
<tag id="1790" title="сокровища"/>
<tag id="1811" title="нечисть"/>
</item>
This may not be the best design, however. Do you really want to allow empty <item>
s or <item>
s containing only tags and director? If not, you might consider an alternative design or using XSD 1.1.
An alternative design
If you are designing this schema, a better solution would be to change the design and accommodate the repeating elements in wrapper elements. It's a better design because it represents the elements as a group, or array, which is what they actually are. It makes it easier to process them, use them in language-mapping software, etc.
If you can change the design, an alternative design could be a schema which would allow something like this:
<item id="17314" ... >
<tariffs>
<tariff .../>
<tariff .../>
</tariffs>
<categories>
<category id="71" title="Фильмы"/>
<category id="72" title="театрь"/>
</categories>
<directors>
<director id="16910" title="Константин Станиславский"/>
<director id="16911" title="Вуди Аллен"/>
<director id="16912" title="Педро Альмодовар"/>
</directors>
<tags>
<tag id="752" title="деньги"/>
<tag id="894" title="любовь"/>
<tag id="1164" title="вещь"/>
</tags>
<countries>
<country id="2543" title="Франция"/>
<country id="2600" title="Испания"/>
<country id="8789" title="Россия"/>
</countries>
<year id="227" title="2003"/>
<genres>
<genre id="674" title="художественное"/>
<genre id="690" title="боевик"/>
<genre id="702" title="приключения"/>
<genre id="5938" title="фэнтези"/>
<genre id="6467" title="фестивальное"/>
<genre id="8964" title="HD фильмы"/>
</genres>
</item>
Where you could use xs:all
to allow each collection to occur at most once in <item>
, in any order (allowing for optional collections, such as tariff
):
<xs:element name="item">
<xs:complexType>
<xs:all>
<xs:element ref="tariffs" minOccurs="0"/>
<xs:element ref="categories"/>
<xs:element ref="directors"/>
<xs:element ref="tags"/>
<xs:element ref="countries"/>
<xs:element ref="year"/>
<xs:element ref="genres"/>
</xs:all>
<xs:attribute type="xs:short" name="id" use="optional"/>
...
</xs:complexType>
</xs:element>
XSD 1.1
You can't have xs:all
with unbounded elements in XSD 1.0. As I showed in the beginning of this answer, your solution doesn't need it since all elements in <item>
are optional. But if you actually need to require elements in <item>
, can't change your design and still want to allow elements in any order, you can validate your code using the same schema you provided, as long as you use it with XSD 1.1.
To do that you will need to use a parser that supports XSD 1.1 and to add two attributes to your <xs:schema>
root element, as shown below:
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:vc="http://www.w3.org/2007/XMLSchema-versioning" vc:minVersion="1.1">
...
Then you can replace the <xs:sequence>
in the declaration for the <item>
element in your XSD for <xs:all>
and it will work when validated in a parser that supports XSD 1.1.