问题描述
我有以下两个 XML 文档:
I have the following two XML documents:
<struct>
<type>a</type>
<p1 xsi:nil="true"/>
<p2 xsi:nil="true"/>
</struct>
<struct>
<type>b</type>
<p1 xsi:nil="true"/>
<p2 xsi:nil="true"/>
</struct>
我希望构建一个模式来验证如果元素类型的值为a",那么它是子元素(又名 p1
& p2
)应该为零或空.而当元素类型是其他类型时,例如b",则 p1
或 p2
元素可以包含任何内容或不包含任何内容.
I wish to build a schema which validates that in case the value of the element type is "a", then it's sub elements (aka p1
& p2
) should be nil or empty.Whereas when the element type is something else, such as "b", then p1
or p2
elements can contain anything or nothing.
推荐答案
这是共现约束"的经典示例(一个元素的类型取决于另一个元素的值).这不能用 XSD 1.0 来完成,但可以通过断言使用 XSD 1.1 来完成
That's a classic example of a "co-occurrence constraint" (the type of one element depends on the value of another). This can't be done with XSD 1.0, but it can be done using XSD 1.1 by means of assertions
<xs:assert test="if (type eq 'a') then nilled(p1) else true()"/>
这篇关于根据元素值改变 XSD 中的类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!