Maverick Riz
Maverick Riz

Reputation: 2065

Java - how to specify xsd element depends on another element

I feel like this is a simple problem, but i couldn't find any solutions. In my xsd, i have a request object which is a complex element and has 3 elements in it. Ex: GetApplicationRequest has element 1 (name), element 2 (dob), element 3 (license_id). To create a valid request, either of (elemen1 and element 2) or (element 3) are required. So if you create a request with element 3, the other 2 shouldn't be there , and if you create a request with element 1 then element 2 is required and element 3 should not be there.

I can use a choice tag here, but that would mean only one of the 3 elements are required. I need a choice and dependency here. So a choice between element 1 and element 3, and if element 1 is being passed, then element 2 is also required.

To give the background, currently this request object has only element 1 and element 2, and both are required. I am adding element 3 here. I don't want to create another complex type for (Element 1 and Element 2) . This is because, this service is already being used in many places and i have to change the request object in those places.

Appreciate your help.

Upvotes: 2

Views: 136

Answers (1)

Michael Glavassevich
Michael Glavassevich

Reputation: 1040

Based on what you described it seems a complex type like this would satisfy your requirement:

<xs:complexType name="GetApplicationRequest">
 <xs:choice>
  <xs:sequence>
   <xs:element ref="name"/>
   <xs:element ref="dob"/>
  </xs:sequence>
  <xs:element ref="license_id"/>
 </xs:choice>
</xs:complexType>

Upvotes: 3

Related Questions