问题
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?
回答1:
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:
- The above solution works for XSD 1.1 only. Your requirement runs afoul of Unique Particle Attribution under XSD 1.0.
- By default
xs:any
hasprocessContents="strict"
. See processContents strict vs lax vs skip for xsd:any for an explanation of the various values forprocessContents
.
来源:https://stackoverflow.com/questions/37486498/xsdany-other-elements-inside-choice