简体   繁体   English

Java-如何指定xsd元素取决于另一个元素

[英]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. 在我的xsd中,我有一个request对象,它是一个复杂元素,其中有3个元素。 Ex: GetApplicationRequest has element 1 (name), element 2 (dob), element 3 (license_id). 例如:GetApplicationRequest具有元素1(名称),元素2(dob),元素3(license_id)。 To create a valid request, either of (elemen1 and element 2) or (element 3) are required. 要创建一个有效的请求,需要(元素1和元素2)或(元素3)。 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. 因此,如果您使用元素3创建一个请求,则其他2不应该存在,如果您使用元素1创建一个请求,则元素2是必需的,元素3不应该存在。

I can use a choice tag here, but that would mean only one of the 3 elements are required. 我可以在此处使用选择标记,但这意味着仅需要3个元素之一。 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. 因此,在元素1和元素3之间进行选择,如果要传递元素1,则还需要元素2。

To give the background, currently this request object has only element 1 and element 2, and both are required. 为了提供背景信息,当前该请求对象只有元素1和元素2,并且两者都是必需的。 I am adding element 3 here. 我在这里添加元素3。 I don't want to create another complex type for (Element 1 and Element 2) . 我不想为(元素1和元素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. 感谢您的帮助。

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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM