Reputation: 13240
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:
<child>
should be unbounded <child>
should be 0 (i.e. it should not appear)Is it possible to enforce these kind of rules with XSD?
Upvotes: 2
Views: 432
Reputation: 163322
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()"
Upvotes: 2