Gabu Gabu - 4 months ago 10
Java Question

Handle all random occurring element in single element into xsd

I'm define one element in xsd file like below

<xs:element name="NonTeachingActivity">
<xs:complexType>
<xs:sequence>
<xs:element name="NTCode" type="String10"/>
<xs:element name="Description" type="String100"/>
<xs:element name="Type" type="MeetingType"/>
</xs:sequence>
<xs:attribute name="RefId" type="GUID" use="required"/>
</xs:complexType>
</xs:element>


in this element sequence of element is not necessary follow order,
tag will be


1) NTCode,Description,Type 2) Description,NTCode,Type 3)
Type,NTCode,Description


so it is possible to handle all the order in given(NonTeachingActivity) single element ?

Answer

With XSD 1.0 there is two possible solutions:

  1. Use choice inside your sequence:

    <xs:element name="NonTeachingActivity">
        <xs:complexType>
            <xs:sequence>
                <xs:choice minOccurs="0" maxOccurs="unbounded">
                    <xs:element name="NTCode" type="String10"/>
                    <xs:element name="Description" type="String100"/>
                    <xs:element name="Type" type="MeetingType"/>
                </xs:choice>
            </xs:sequence>
            <xs:attribute name="RefId" type="GUID" use="required"/>
        </xs:complexType>
    </xs:element>
    

    But this will not limit amount of occurrences of each child element.

  2. Another way is to use all:

    <xs:element name="NonTeachingActivity">
        <xs:complexType>
            <xs:all>
                <xs:element name="NTCode" type="String10"/>
                <xs:element name="Description" type="String100"/>
                <xs:element name="Type" type="MeetingType"/>
            </xs:all>
            <xs:attribute name="RefId" type="GUID" use="required"/>
        </xs:complexType>
    </xs:element>
    

    But each child element can occur zero or one time.

Comments