xsdminoccurs

XSD Make minOccurs depend on containing type


I have a complex type defined which doesn't currently contain any minOccurs restrictions. When I use this comlpex type as an element type I sometimes want the elements to have minOccurs 0, other times 1. E.g.

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

Is this possible?

Thanks in advance.


Solution

  • Groups are your friend, e.g.

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