2009-05-19 52 views
5

XML模式指定「發生指標」(maxOccurrence,minOccurrence)。是否有一個「最佳實踐」,其中應使用這些指標的元素(xsd:element,xsd:sequence或xsd:all)?使用XSD發生指標

例子:

要麼

 

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

 

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

我想在到達:

 

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

回答

6

我發現自己。

我的例子之間的差異乍一看並不是很明顯,但如果這個例子稍微複雜一些,那就有道理了。

以下擴展澄清的差異:

 

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

導致

 

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

 

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

導致

 

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

 

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

導致

 

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

 

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

導致

 

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

如果序列只包含一個孩子,我想這並不多有效的區別。但是,我會將它放在xsd:元素上:如果序列中有多個元素,通常會放置它們。