Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

xml schema sequence multiple of same elements

Tags:

xml

xsd

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.

like image 732
Stribor50 Avatar asked Feb 24 '26 06:02

Stribor50


1 Answers

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.

like image 180
Ian Roberts Avatar answered Feb 26 '26 22:02

Ian Roberts