1

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?

Kevin Macejko
  • 97
  • 2
  • 11
  • Your intentions are unclear. Are you trying to require one of `Account` or `KK` and then 0 or more of any other elements? Please state exactly what should and should not be allowed. Thanks. – kjhughes May 27 '16 at 15:58
  • Sorry for being uncelar. But yes, you hit the point. That's exactly what I want. – Kevin Macejko May 27 '16 at 16:26
  • Bear in mind that `` does exactly what you way you want (in both XSD 1.0 and in 1.1): it matches an `Account` element, or a `KK` element, or any other element. – C. M. Sperberg-McQueen May 30 '16 at 18:05

1 Answers1

0

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:

Community
  • 1
  • 1
kjhughes
  • 106,133
  • 27
  • 181
  • 240
  • I feel embarrassed now, but because of the language barrier, I didn't understand clearly your suggestion for clarification. Also, I realized any element should be there only once. So I modified the problem statement accordingly. I would appreciate if you can answer my question again. Thanks and sorry for the confusion. – Kevin Macejko May 27 '16 at 21:39
  • No problem. Answer updated to match your question update. – kjhughes May 28 '16 at 04:01
  • Thank you. So the solution is to use the higher version of XML Schema. Also, it seems that In version 1.1. there is no need for sequence. Choice inside the complex type works Ok as well. – Kevin Macejko May 30 '16 at 09:36
  • You're right -- there is a superfluous `xsd:sequence`. Fixed. Thanks. – kjhughes May 30 '16 at 13:08
  • Today, I found out, that the schema as you suggested is valid, but there is validation problem when this schema is included in WSDL file. Altova returns the following error: Invalid XML schema: **'The content model of complex type definition '{anonymous}' is ambiguous.' .** Do you have any idea how to solve this problem? Thank you – Kevin Macejko Feb 02 '17 at 17:52
  • @KevinMacejko: Please post a new question and be sure to include a [mcve] illustrating the problem. In the meantime, **make sure you have XSD 1.1 support enabled**, however that's done in Altova -- I don't have or use that tool. Thanks. – kjhughes Feb 02 '17 at 18:48