开发者

XML Schema to Limit Element Occurrence Based on Another Element's Value

Is it possible to define an XML Schema that constrains the occurrence of one or more elements based on another element's value?

For example, in the XML below, when the TYPE el开发者_JAVA技巧ement has a value of 'A', then there can be only one ITEM_TYPE_1 element but at least two ITEM_TYPE_2 elements.

<ROOT>
    <TYPE>A</TYPE>
    <ITEM_TYPE_1 />
    <ITEM_TYPE_2 />
    <ITEM_TYPE_2 />
</ROOT>

Conversely, when the TYPE element has a value of 'B', then there must be at least two ITEM_TYPE_1 elements, but only one ITEM_TYPE_2 element.

<ROOT>
    <TYPE>B</TYPE>
    <ITEM_TYPE_1 />
    <ITEM_TYPE_1 />
    <ITEM_TYPE_2 />
</ROOT>

Not sure if I'm trying to do something that XML Schema can't handle or not, so feedback is appreciated.


No, XML Schema 1.0 does not support such constraints, where there is a dependency on element or attribute values. XML Schema 1.1 is not yet widely adopted but supports value constraints via assertions.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜