XML Schema (XSD) - How do I specify a parent to host at least one child?

I have an XML Schema (XSD) that defines an element as required (name it parent); this parent has, say, five children, which may be optional, BUT CANNOT BE one child.

How can I specify this in xsd?

To clarify: children are different elements and optional. For instance.

<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 if each child is optional, the parent must have at least one child.

+5
source share
4 answers

There is always a 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>
+4
source

Use minOccurs for example

<xsd:complexType name="Parent">
  <xsd:sequence>
    <xsd:element minOccurs="1" maxOccurs="5" name="Child" type="q10:string"/>
    </xsd:sequence>
</xsd:complexType>
0
source

You can create a replacement group that includes all of your children. To do this, you use the attribute minOccursto indicate that the document must have at least one group element.

0
source

Using statements (I think this is only available in XSD 1.1), you can do the following:

<xs:element name="Parent">
    <xs:complexType>
        <xs:sequence>
            <xs:element name="Child1" type="xs:string" minOccurs="0"/>
            <xs:element name="Child2" minOccurs="0"/>
            <xs:element name="Child3" minOccurs="0"/>
        </xs:sequence>
        <xs:assert test="count(*)>=1"/>
    </xs:complexType>
</xs:element>
0
source

All Articles