Reputation: 328
I have the following XSD:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="PaymentSearchRequest">
<xs:complexType>
<xs:sequence>
<xs:element name="User">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:minLength value="1" />
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="Password" nillable="false">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:minLength value="1" />
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="Key" minOccurs="0">
<xs:simpleType>
<xs:restriction base="xs:string" />
</xs:simpleType>
</xs:element>
<xs:element name="Iteration" default="0000" minOccurs="0">
<xs:simpleType>
<xs:restriction base="xs:short" />
</xs:simpleType>
</xs:element>
<xs:element name="ReturnUrl" minOccurs="0">
<xs:simpleType>
<xs:restriction base="xs:anyURI">
<xs:minLength value="1" />
<xs:pattern value="[hH][tT]{2}[pP]://[wW]{3}.*" />
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="PaymentSearch" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="SessionId">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:minLength value="5" />
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="ConfirmationNumber" minOccurs="0" >
<xs:simpleType>
<xs:restriction base="xs:string" />
</xs:simpleType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
This was working fine with our old business logic. And the following XML was being validated:
<PaymentSearchRequest>
<User>[email protected]</User>
<Password>test@1</Password>
<PaymentSearch>
<SessionId>79C14E66-87E1-42BD-9974-096E2D037D22</SessionId>
<ConfirmationNumber />
</PaymentSearch>
</PaymentSearchRequest>
Now I need to cater the following scenarios:
1)
<PaymentSearchRequest>
<User>[email protected]</User>
<Password>test@1</Password>
<PaymentSearch>
<ConfirmationNumber>HUS73945KJAF</ConfirmationNumber>
</PaymentSearch>
</PaymentSearchRequest>
2)
<PaymentSearchRequest>
<User>[email protected]</User>
<Password>test@1</Password>
<PaymentSearch>
<SessionId>79C14E66-87E1-42BD-9974-096E2D037D22</SessionId>
</PaymentSearch>
</PaymentSearchRequest>
3)
<PaymentSearchRequest>
<User>[email protected]</User>
<Password>test@1</Password>
<PaymentSearch>
<SessionId>79C14E66-87E1-42BD-9974-096E2D037D22</SessionId>
<ConfirmationNumber>AJKF3894KJ3425</ConfirmationNumber>
</PaymentSearch>
</PaymentSearchRequest>
What I have tried so far:
Choice element The choice element approach is failing for the third scenario.
Upvotes: 0
Views: 1115
Reputation: 89295
The following xsd using xs:choice
and successfully validate all of the 3 possible PaymentSearch
scenarios you posted :
<xs:element name="PaymentSearch" maxOccurs="unbounded">
<xs:complexType>
<xs:choice minOccurs="1" maxOccurs="unbounded">
<xs:element name="SessionId">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:minLength value="5" />
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="ConfirmationNumber">
<xs:simpleType>
<xs:restriction base="xs:string" />
</xs:simpleType>
</xs:element>
</xs:choice>
</xs:complexType>
</xs:element>
You can change maxOccurs
of xs:choice
to 2
if each of ConfirmationNumber
and SessionId
can only occur once (so that maximum occurrence of the two is 2
).
Upvotes: 1