On an XSD element, can I set maxOccurs depending on a grandparent node's attribute value?
I want to define the two xml structures below using a single XSD.
<grandparent action="SUBMIT">
<parent>
<child value="blah"/>
</parent>
<grandparent>
<grandparent action="CANCEL">
<parent>
</parent>
<grandparent>
Rules I want to enforce:
- If action equals "SUBMIT", maxOccurs for
<child>
should be unbounded - If action equals "CANCEL", maxOccurs for
<child>
should be 0 (i.e. it should not app开发者_运维百科ear)
Is it possible to enforce these kind of rules with XSD?
You can't do this kind of stuff in XSD 1.0 - but you can in XSD 1.1 (implemented currently in recent releases of Saxon and Xerces). There are two possible approaches:
(a) Conditional type assignment: have two different types for grandparent, and select which one to validate against base on the attribute value
(b) assertions: leave maxOccurs unbounded in the grammar, and add an assertion to grandparent along the lines test="if (@ACTION='cancel') then empty(*/child) else true()"
精彩评论