views:

427

answers:

2

XML Schema specifies "occurrence indicators" (maxOccurrence, minOccurrence). Is there a "best pratice" 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>

A: 

If your sequence only contains one child, I suppose it doesn't make much effective difference. I would put it on the xsd:element, though: that's where I'd normally put them if there was more than one element in the sequence.

araqnid
A: 

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 differnces:


<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>

Dynamicbyte