XSD Make minOccurs取决于包含的类型

时间:2009-10-16 14:50:11

标签: xsd minoccurs

我定义了一个复杂类型,它当前不包含任何minOccurs限制。当我使用这个comlpex类型作为元素类型时,我有时希望元素具有minOccurs 0,其他时间为1.例如。

<xsd:complexType name="Identifier"> 
    <xsd:sequence> 
        <xsd:element name="Id" type="xsd:string"/> 
        <xsd:element name="Version" type="xsd:string"/> 
    </xsd:sequence> 
</xsd:complexType> 

<xsd:complexType name="Wibble"> 
    <xsd:sequence> 
        <xsd:element name="Id" type="Identifier"/> <!-- I want all elements of Identifier to be mandatory when used as part of a 'Wibble' -->
    </xsd:sequence> 
</xsd:complexType> 


<xsd:complexType name="Wobble"> 
    <xsd:sequence> 
        <xsd:element name="Id" type="Identifier"/> <!-- I want all elements of Identifier to be optional when used as part of a 'Wobble' -->
    </xsd:sequence> 
</xsd:complexType> 

这可能吗?

提前致谢。

1 个答案:

答案 0 :(得分:1)

群组是你的朋友,例如

<xsd:group name="IdentifierGroup">
   <xsd:sequence> 
      <xsd:element name="Id" type="Identifier"/>
   </xsd:sequence>
</xsd:group>

<xsd:complexType name="Wibble"> 
    <xsd:sequence> 
        <xsd:group ref="IdentifierGroup" minOccurs="1"/>
        <!-- more elements for Wibble here -->
    </xsd:sequence> 
</xsd:complexType> 

<xsd:complexType name="Wobble"> 
    <xsd:sequence> 
        <xsd:group ref="IdentifierGroup" minOccurs="0"/>
        <!-- more elements for Wobble here -->
    </xsd:sequence> 
</xsd:complexType>