Daniel Alexiuc
Daniel Alexiuc

Reputation: 13240

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:

Is it possible to enforce these kind of rules with XSD?

Upvotes: 2

Views: 432

Answers (1)

Michael Kay
Michael Kay

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

Related Questions