Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XML Schema to match the following ("all", with unbounded maxOccurs?)

Tags:

xsd

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.

like image 620
desau Avatar asked Sep 30 '10 03:09

desau


People also ask

What is maxOccurs unbounded?

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.

What is maxOccurs in XML?

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.

What is unbounded XML?

XML Schema is used to validate the XML document. "unbounded" means that an upper limit on the number of elements will not be imposed.

Is maxOccurs unbounded insecure in XSD?

No. There's nothing inherently wrong with using maxOccurs="unbounded" in XSD.


2 Answers

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!

like image 143
desau Avatar answered Dec 24 '22 15:12

desau


<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".

like image 26
Emre Tapcı Avatar answered Dec 24 '22 15:12

Emre Tapcı