Reputation: 41
I am trying to understand example from w3 school about sequence element. I thought that element inside the sequence can show only once but example from w3 school lead me to believe otherwise. This is example...
<xs:element name="pets">
<xs:complexType>
<xs:sequence minOccurs="0" maxOccurs="unbounded">
<xs:element name="dog" type="xs:string"/>
<xs:element name="cat" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
So explanation on w3 schools says that "This example shows a declaration for an element called "pets" that can have zero or more of the following elements, dog and cat, in the sequence element". I tried to test that by doing this....
sorry second example i pasted by mistake....this is what i meant that wil not validate based on schema from the top...
<pets>
<dog>something</dog>
<dog>something else</dog>
<cat>else</cat>
</pets>
But validator gives me errors http://www.corefiling.com/opensource/schemaValidate.html. I would like to understand both examples. Sequence where it is allowable to have multiple of same elements and also where each element in sequence must be present and must show once only.
any examples and suggestions would be appreciated.
Upvotes: 3
Views: 5814
Reputation: 23627
The minOccurs
/maxOccurs
constraints were declared in the sequence element, so they apply to a sequence
of (dog, cat)
i.e. one dog
, followed by one cat. You declared that the sequence can repeat, not the individual elements. So your schema will validate documents like this, for example:
<pets xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="schema6.xsd">
<dog>something</dog> <!-- sequence 1 -->
<cat>else</cat>
<dog>something</dog> <!-- sequence 1 -->
<cat>else</cat>
<dog>something</dog> <!-- sequence 1 -->
<cat>else</cat>
</pets>
The document you posted will not validate because it includes a dog
element which does not belong to the sequence allowed:
<pets>
<dog>something</dog> <!-- not allowed - where is the following cat? -->
<dog>something else</dog> <!-- sequence dog,cat - OK -->
<cat>else</cat>
</pets>
Your schema accepts empty sequences (no cat
s or dog
s) and also unbounded sequences (any amount of cat
s and dog
s), but there must always be a cat
if there is a dog
, and a dog
if there is a cat
, and the dog
must come before the cat
. It's a very strict rule.
If you want to accept any number of cat
s and dog
s in any order, you have to redefine those constraints in the individual element declarations, since they are using the defaults and the default is minOccurs="1"
maxOccurs="1"
:
<xs:sequence minOccurs="0" maxOccurs="unbounded">
<xs:element name="dog" type="xs:string" minOccurs="0"/>
<xs:element name="cat" type="xs:string" minOccurs="0"/>
</xs:sequence>
Now you allow 0 cat
s or dog
s in each sequence, so you can add dog
s and cat
s in any order, have only cat
s, have only dog
s, have empty sequences, have no sequences...
<pets>
<cat>else</cat>
<cat>else</cat>
<dog>something</dog>
<cat>else</cat>
<dog>something</dog>
<dog>something</dog>
<dog>something</dog>
</pets>
(You can also obtain the same result with <xs:choice>
as shown in the other answer).
Upvotes: 2
Reputation: 122414
That schema allows the content to be zero or more repetitions of the sequence "<dog>
followed by <cat>
". In other words the pets
element can be empty, or it can contain dog-cat, dog-cat-dog-cat, etc. but not dog-dog.
If you want any number of dog or cat elements in any combination then you probably want a choice
instead of a sequence
.
<xs:element name="pets">
<xs:complexType>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="dog" type="xs:string"/>
<xs:element name="cat" type="xs:string"/>
</xs:choice>
</xs:complexType>
</xs:element>
This would allow zero or more repetitions of X
, where X
is either one dog or one cat element.
Upvotes: 3