以 xsd 中的值为条件 [英] Conditional on value in xsd
问题描述
有没有办法在标签值上添加条件?例如,我的 xml 看起来像这样:
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>
我需要一个 XSD 方案,在我的 xml 中允许/不允许这些条件.
I need a XSD Scheme that allow/not allows these conditions in my xml.
谢谢.
推荐答案
如果 DateTimeType 是一个属性,则可以使用条件类型分配方便地完成此操作.但是如果它必须是一个子元素,你可以使用断言来实现,例如
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.
这篇关于以 xsd 中的值为条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!