Conditional on value in xsd

巧了我就是萌 提交于 2019-12-11 16:36:02

问题


is there any way to add conditions on a tag value? For example, my xml looks like this:

<Root>
 <Scheduler>
  <DateTimeType>DAY</DateTimeType>
  <DayOfWeek>Sunday</DayOfWeek> <!-- TAG IS ALLOWED -->
 </Scheduler>

 <Scheduler>
  <DateTimeType>MONTH</DateTimeType>
  <DayOfWeek>Sunday</DayOfWeek> <!-- TAG IS NOT ALLOWED -->
  <DayOfMonth>28</DayOfMonth> <!-- TAG IS ALLOWED -->
 </Scheduler>

 <Scheduler>
  <DateTimeType>WEEKDAY</DateTimeType>
  <DayOfWeek>Sunday</DayOfWeek> <!-- TAG IS ALLOWED -->
  <TimeOfDay>15:26</TimeOfDay> <!-- TAG IS ALLOWED -->
  <DayOfMonth>28</DayOfMonth> <!-- TAG IS NOT ALLOWED -->
 </Scheduler>

 <Scheduler>
  <DateTimeType>TIME</DateTimeType>
  <DayOfWeek>Sunday</DayOfWeek> <!-- TAG IS NOT ALLOWED -->
  <DayOfMonth>28</DayOfMonth> <!-- TAG IS NOT ALLOWED -->
  <TimeOfDay>15:26</TimeOfDay> <!-- TAG IS ALLOWED -->
 </Scheduler>
</Root>

I need a XSD Scheme that allow/not allows these conditions in my xml.

Thanks.


回答1:


If DateTimeType were an attribute, this could be done conveniently using conditional type assignment. But if it has to be a child element, you can do it using assertions, for example

<xs:assertion test="not(DateTimeType = 'MONTH' and exists(DayOfWeek)"/>

By the way, they are not called tags, they are called elements. An element generally has two tags, a start tag and an end tag. Using the correct terminology has many benefits, for example you'll find that you start to understand the language used in error messages better.



来源:https://stackoverflow.com/questions/25658953/conditional-on-value-in-xsd

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!