Say I have an element, call it <A>
. <A>
can have children types of <B>
and <C>
. Now - here's the twist. Any number of <B>
and <C>
children can live in <A>
, in any order.
For example:
<A>
<C>
<C>
<B>
<C>
<B>
<B>
<C>
...
</A>
Is there a schema rule that fits this? Seems like "all" would work, if I could put maxOccurs="unbounded", but I guess that's not legal.
The maximum number of times an element may appear is determined by the value of a maxOccurs attribute in its declaration. This value may be a positive integer such as 41, or the term unbounded to indicate there is no maximum number of occurrences.
The maxOccurs attribute specifies the maximum number of times that the element can occur. It can have a value of any positive integer greater than or equal to the value of the minOccurs attribute.
XML Schema is used to validate the XML document. "unbounded" means that an upper limit on the number of elements will not be imposed.
No. There's nothing inherently wrong with using maxOccurs="unbounded" in XSD.
Answering my own question -- looks like trang (http://www.thaiopensource.com/relaxng/trang.html) gave me the anwer:
<xs:element name="A">
<xs:complexType>
<xs:choice maxOccurs="unbounded">
<xs:element ref="B"/>
<xs:element ref="C"/>
</xs:choice>
</xs:complexType>
</xs:element>
Very cool!
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="root" type="root"/>
<xs:complexType name="root">
<xs:choice minOccurs="0">
<xs:element name="a"/>
</xs:choice>
</xs:complexType>
</xs:schema>
This schema validates
<root>
</root>
But if you omit minOccurs="0"
of <xs:choice>
, it does not.
It validates
<root>
<a/>
</root>
without minOccurs="0"
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With