问题
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