user2156088
user2156088

Reputation: 2420

Parents element mandatory but not child in xml

I have a parent element called ContactNumber which is mandatory.It has 4 child elements(mobile,work,fax,home) that is not mandatory.But atleast need to get one value for the parent from any of these 4 child.How should i do this?.

   <xs:element name="contactDetails">
    <xs:complexType> 
   <xs:element name="jobTitle" /> 
  <xs:sequence><xs:element name="contactNumber" type="contactNumberInfo"  minOccurs="1" maxOccurs="3" />        
    </xs:sequence>
 </xs:complexType>
  </xs:element>
 <xs:complexType name="contactNumberInfo">
    <xs:sequence>
        <xs:element name="mobile">
            <xs:simpleType>
                <xs:restriction base="xs:positiveInteger">
                    <xs:pattern value="[0-9]{10}" />
                </xs:restriction>
            </xs:simpleType>
        </xs:element>           

        <xs:element name="home" type="xs:positiveInteger" />
        <xs:element name="work" type="xs:positiveInteger" />
        <xs:element name="fax" type="xs:positiveInteger" />
    </xs:sequence>
</xs:complexType>

Upvotes: 0

Views: 169

Answers (1)

xystum
xystum

Reputation: 1009

If there is this fixed sequence of elements, its similar to XML schema construct for "any one or more of these elements but must be at least one".

Try:

<xs:element name="ContactNumber">
        <xs:complexType>
            <xs:choice>
                <xs:sequence>
                    <xs:element name="home" type="xs:positiveInteger" minOccurs="1" maxOccurs="1" />
                    <xs:element name="work" type="xs:positiveInteger"  minOccurs="0" maxOccurs="1" />
                    <xs:element name="fax" type="xs:positiveInteger"  minOccurs="0" maxOccurs="1" />
                    <xs:element name="mobile" type="xs:positiveInteger"  minOccurs="0" maxOccurs="1" />
                </xs:sequence>
                <xs:sequence>
                    <xs:element name="work" type="xs:positiveInteger"  minOccurs="1" maxOccurs="1" />
                    <xs:element name="fax" type="xs:positiveInteger"  minOccurs="0" maxOccurs="1" />
                    <xs:element name="mobile" type="xs:positiveInteger"  minOccurs="0" maxOccurs="1" />
                </xs:sequence>
                <xs:sequence>
                    <xs:element name="fax" type="xs:positiveInteger"  minOccurs="1" maxOccurs="1" />
                    <xs:element name="mobile" type="xs:positiveInteger"  minOccurs="0" maxOccurs="1" />
                </xs:sequence>
                <xs:sequence>
                    <xs:element name="mobile" type="xs:positiveInteger"  minOccurs="1" maxOccurs="1" />
                </xs:sequence>
            </xs:choice>
        </xs:complexType>
    </xs:element>

Upvotes: 1

Related Questions