Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Usage of XSD occurrence Indicators

Tags:

xml

xsd

XML Schema specifies "occurrence indicators" (maxOccurrence, minOccurrence). Is there a "best practice" in which elements (xsd:element, xsd:sequence or xsd:all) these indicators should be used?

Example:

either


<xsd:element name="XList">
    <xsd:complexType>
        <xsd:sequence>
            <xsd:element name="X" type="xsd:token" minOccurs="1" maxOccurs="unbounded" />
        </xsd:sequence>
    </xsd:complexType>
</xsd:element>

or


<xsd:element name="XList">
    <xsd:complexType>
        <xsd:sequence minOccurs="1" maxOccurs="unbounded">
            <xsd:element name="X" type="xsd:token" />
        </xsd:sequence>
    </xsd:complexType>
</xsd:element>

I want to arrive at:


<XList>
    <X>First</X>
    <X>Second</X>
    <X>Third</X>
    <X>Fourth</X>
    <X>Fifth</X>
...
</XList>

like image 230
Dynamicbyte Avatar asked May 19 '09 13:05

Dynamicbyte


1 Answers

I found out myself.

The difference between my examples is not very obvious on the first glance, but if the example had been a little more complex it would have made sense.

The following extension clarifies the differences:


<xsd:element name="XList">
    <xsd:complexType>
        <xsd:sequence>
            <xsd:element name="X" type="xsd:token"
                         minOccurs="1" maxOccurs="1"/>
            <xsd:element name="Y" type="xsd:token"
                         minOccurs="1" maxOccurs="1"/>   
        </xsd:sequence>
    </xsd:complexType>
</xsd:element>

leads to


<XList>
    <X>Only X</X>
    <Y>Only Y</Y>
</XList>

whereas


<xsd:element name="XList">
    <xsd:complexType>
        <xsd:sequence>
            <xsd:element name="X" type="xsd:token"
                         minOccurs="1" maxOccurs="unbounded"/>
            <xsd:element name="Y" type="xsd:token"
                         minOccurs="1" maxOccurs="unbounded"/>   
        </xsd:sequence>
    </xsd:complexType>
</xsd:element>

leads to


<XList>
    <X>First X</X>
    <X>Second X</X>
    <X>Third X</X>
    ...
    <Y>First Y</Y>
    <Y>Second Y</Y>
    ...
</XList>

and


<xsd:element name="XList">
    <xsd:complexType>
        <xsd:sequence minOccurs="1" maxOccurs="unbounded" >
            <xsd:element name="X" type="xsd:token"
                         minOccurs="2" maxOccurs="2"/>
            <xsd:element name="Y" type="xsd:token"
                         minOccurs="1" maxOccurs="1"/>   
        </xsd:sequence>
    </xsd:complexType>
</xsd:element>

leads to


<XList>
    <X>First X</X>
    <X>Second X</X>
    <Y>First Y</Y>
    <X>Third X</X>
    <X>Fourth X</X>
    <Y>Second Y</Y>
    ...
</XList>

and


<xsd:element name="XList">
    <xsd:complexType>
        <xsd:sequence minOccurs="1" maxOccurs="unbounded" >
            <xsd:element name="X" type="xsd:token"
                         minOccurs="1" maxOccurs="unbounded"/>
            <xsd:element name="Y" type="xsd:token"
                         minOccurs="1" maxOccurs="unbounded"/>   
        </xsd:sequence>
    </xsd:complexType>
</xsd:element>

leads to


<XList>
    <X>First X</X>
    <X>Second X</X>
    <X>Third X</X>
    ...
    <Y>First Y</Y>
    <Y>Second Y</Y>
    ...
    <X>Fourth X</X>
    <X>Fifth X</X>
    <X>Sixth X</X>
    ...
    <Y>Third Y</Y>
    <Y>Fourth Y</Y>
    ...
</XList>

like image 120
Dynamicbyte Avatar answered Nov 06 '22 05:11

Dynamicbyte