Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XML Schema: choice and minOccurs

Tags:

xsd

In this example below,

        <xs:complexType>
        <xs:choice minOccurs="3" maxOccurs="unbounded">
            <xs:element ref="Start"/>
            <xs:element ref="Center"/>
            <xs:element ref="End"/>
            <xs:element ref="PI" minOccurs="0"/>
            <xs:element ref="Feature" minOccurs="0" maxOccurs="unbounded"/>
        </xs:choice>
        </xs:complexType>

What happens when choice has minOccurs > 1? Does this mean element "Start" can occur 3 times?

like image 549
DynamicScope Avatar asked Dec 21 '12 06:12

DynamicScope


People also ask

What is minOccurs in XML?

An array with a varying number of elements is represented in the XML schema by using the minOccurs and maxOccurs attributes on the element declaration: 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.

How do you create a choice in XML schemas?

Using <xsd:choice> in an XSD It has to be defined in XML schema definition of a nested XML content. The element in the root schema has to be optional. Add attribute minOccurs="0" on the element to make it optional.

What is choice element in XML?

Definition and Usage XML Schema choice element allows only one of the elements contained in the <choice> declaration to be present within the containing element.

What is a choice element?

<xsd:choice> ElementAllows one and only one of the elements contained in the selected group to be present within the containing element. Copy. <choice id = ID maxOccurs= (nonNegativeInteger | unbounded) : 1 minOccurs= nonNegativeInteger : 1 {any attributes with non-schema Namespace}...> Content: (annotation?, (


1 Answers

What happens when choice has minOccurs > 1? Does this mean element "Start" can occur 3 times?

Yes <Start> can occur 3 or more times! Having minOccurs of choice as more than 1 allows set of elements to appear more than once or different elements appear multiple times.

Detailed explanation: In the above example you have applied minOccurs as 3 for <Choice> and maxOccurs as unbounded! That means .. Child elements listed under <Choice>, any three of them or any tag repeated three times should appear under their parent! sample XML are listed below:

Let us assume that these set of tags appear under a node called <parent> then:

  <parent>
    <Start>Start1</Start>
    <Center>Center1</Center>
    <End>End1</End>
  </parent>

-------- OR --------

  <parent>
    <Center>Start1</Center>
    <Center>Center1</Center>
    <Feature>End1</Feature>
  </parent>

-------- OR --------

 <parent>
    <Start>Start1</Start>
    <Start>Start1</Start>
    <Start>Start1</Start>
    <Start>Start1</Start>
  </parent>

all the above combinations are valid!

BUT you have also defined minOccurs="0" for 'PI' and 'Feature' ..

This addition causes Validator to pass parent with no child elements as well. ie:

  <parent>
  </parent>

If you remove minOccurs from those two elements then validation forces you to include minimum of 3 tags to be included under parent.

Also having maxOccurs="unbounded" for Feature Element is of no use! The behavior won't change if you add it or take it off..

like image 158
InfantPro'Aravind' Avatar answered Sep 21 '22 02:09

InfantPro'Aravind'