13

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.

Kasun Gajasinghe
  • 2,735
  • 1
  • 21
  • 30
desau
  • 3,001
  • 1
  • 24
  • 27

2 Answers2

30

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!

desau
  • 3,001
  • 1
  • 24
  • 27
  • 7
    Helpful, thanks. Any idea if it's possible to do something similar that will enforce the existence of at least one and at least one while allowing any number of each, in any order? – PaulF Oct 10 '11 at 18:03
1
<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".

Emre Tapcı
  • 1,743
  • 17
  • 16
  • Your answer might be right but is not working code. Please improve your answer to working "copy/paste" code by editing your answer yourself. – ZF007 Mar 08 '18 at 08:26
  • What I meant.... make it a working answer... similar to that user desau show with his answer [here](https://stackoverflow.com/a/3827606/8928024). Anyone who doesn't have as much as experience as you do...will not grasp it directly. So it should be "idiot" proof answer with enough context. – ZF007 Mar 15 '18 at 13:49
  • embed the freeformatter link into your answer... it improves the quality of your answer. And remove comment afterwards... which I'll do with this as well. – ZF007 Mar 15 '18 at 13:53