Search code examples
xmlxsdxsd-validationbackwards-compatibilityxml-validation

xsd:any + other elements inside choice


How can I achieve something like this:

<xs:element name="getSubjectProductsResponse">
   <xs:complexType>
        <xs:sequence minOccurs="0" maxOccurs="unbounded">
            <xs:element name="Products">
                <xs:complexType>
                    <xs:sequence>
                        <xs:element name="Date" type="xs:date"/>
                        <xs:element name="Branch" type="xs:date" minOccurs="0"/>
                        <xs:element name="State" type="xs:string">

                        <xs:element name="ProductDetail">
                          <xs:complexType>
                             **<xs:choice>
                                  <xs:element name="Account" type="ns:TAccount"/>
                                  <xs:element name="KK" type="ns:TCreditCard"/>
                                  <xs:any/>
                             </xs:choice>**
                          </xs:complexType>
                       </xs:element>

This schema is not valid.

It's the part of the structure of the response message for Product List Service. For every Product in response message, there are common attributes (Date, Branch...) and attributes which are specific for specific type of product(in ProductDetail element). That's the reason for using "choice". So, in ProductDetail, there should be only one product element, either KK, or Account.

But it may happen in the future, that I will need to add another type of product. And when this happens, I don't want to impact consumers who are not interested in this product. I want them to be still able to validate messages with new types of products (without changes in their code).

In short, I am trying to require one of Account or KK OR one xs:any other element.

Is there some way, how can I achieve this in XSD?


Solution

  • XSD 1.1

    This declaration for ProductDetail will allow either Account or KK or xs:any other element:

    <xs:element name="ProductDetail">
      <xs:complexType>
        <xs:choice>
          <xs:element name="Account" type="xs:string"/>
          <xs:element name="KK" type="xs:string"/>
          <xs:any/>
        </xs:choice>
      </xs:complexType>
    </xs:element>
    

    Notes: