tags:

views:

57

answers:

3

I have an XML schema (XSD) that defines an element as mandatory (call it the parent); this parent has, lets say, five child elements, which can all be optional, BUT AT LEAST ONE child element must occur.

How can i specify this in the xsd?

To clarify: The children are different elements and optional. For example.

<Parent>
   <Child1>contents are different to other siblings and arbitrary</Child1>
   <Child2>can be text, a simple element, or another complex element</Child2>
   <Child3>etc.. etc</Child3> 
</Parent>

<xs:complexType name="Parent">
  <xs:sequence>
    <xs:element minOccurs="0" name="Child1" type="xs:string"/>
    <xs:element minOccurs="0" name="Child2" />
    <xs:element minOccurs="0" name="Child3" />
  </xs:sequence>
</xs:complexType>

Even though every child is optional, the parent needs to have at least one child.

A: 

Use minOccurs, e.g.

<xsd:complexType name="Parent">
  <xsd:sequence>
    <xsd:element minOccurs="1" maxOccurs="5" name="Child" type="q10:string"/>
    </xsd:sequence>
</xsd:complexType>
codemeit
Not quite what i intended. Please see updated question that hopefully clarifies the intent.
joedotnot
A: 

You can create a substitution group which incorporates all your child elements. For this you use the minOccurs attribute to specify that at least one element of the group must occur in the document.

swegi
Dont think this will work; substitution group elements must somehow be related or derived from each other; my elements are totally independent as alluded to in my question.
joedotnot
A: 

There is always the direct approach:

<xs:complexType name="Parent">
  <xs:choice>
    <xs:sequence>
      <xs:element name="Child1"/>
      <xs:element name="Child2" minOccurs="0"/>
      <xs:element name="Child3" minOccurs="0"/>
    </xs:sequence>
    <xs:sequence>
      <xs:element name="Child2"/>
      <xs:element name="Child3" minOccurs="0"/>
    </xs:sequence>
    <xs:sequence>
      <xs:element name="Child3"/>
    </xs:sequence>
  </xs:choice>
</xs:complexType>
WReach
Direct approach would probably work; but gets too cumbersome for lots of child elements.
joedotnot