Reputation: 11
I need to define a validation rule under XML Schema 1.0 that allows an element to occur (once) among a set of sibling elements only if another specific sibling has a certain value.
For example, given the instance XML document snippet,
<root>
<parent>
<child1>A</child1>
</parent>
<parent>
<child1>B</child1>
<chlld2>C</child2>
</parent>
</root>
I'd like the rule to allow the child2 element to occur only if the required child1 element has a value of 'B', otherwise, the child1 element should occur by itself under a given parent.
This is quite easy to achieve under XML Schema 1.1 using an xs:assert, but the solution under version 1.0 evades me.
Any insights are most appreciated.
Upvotes: 0
Views: 786
Reputation: 25034
The usual approach in XSD 1.0 is to design the XML differently: if we have one particular value B for child1 which makes the occurrence of child2 possible, then we can split child1 into two element types: child1-notB and child1-B. And since in the case of child1-B we know the value, the value doesn't actually need to be present. The XML becomes:
<root>
<parent>
<child1-notB>A</child1-notB>
</parent>
<parent>
<child1-B/>
<chlld2>C</child2>
</parent>
</root>
It's simple to write a content model in which the parent element contains either a child1-notB or a child1-B followed by an optional child2.
As Dijkgraaf has already observed, the specific design you describe cannot be expressed in XSD 1.0. XSD 1.1 added assertions in part because so many people want designs like the one you describe, in which two quite different elements, which have quite different effects on what is and is not allowed, are nevertheless given the same name so as to mask their difference in meaning, instead of being called by different names to make their difference in semantics explicit.
Upvotes: 2