1

I am trying to create an xsd to validate a bit of xml. I want the xsd to fail if no member elements are found, i thought adding minOccurs="1" would achieve this but for some reason even when passing 0 member elements it passes.

<xs:attributeGroup name="memberAttributes">
    <xs:annotation>
      <xs:documentation>Set of attributes for member element</xs:documentation>
    </xs:annotation>
    <xs:attribute name="employeeid" type="xs:string" use="required"/>
    <xs:attribute name="dob" type="Shortdate" use="required"/>
    <xs:attribute name="gender" type="Gender" use="required"/>
    <xs:attribute name="salary" use="required">
      <xs:simpleType>
        <xs:restriction base="Amount2dp">
          <xs:totalDigits value="13"/>
          <xs:maxInclusive value="99999999999.99"/>
        </xs:restriction>
      </xs:simpleType>
    </xs:attribute>
    <xs:attribute name="postcode" type="Postcode" use="required"/>
  </xs:attributeGroup>

  <xs:element name="member">
    <xs:annotation>
      <xs:documentation>Member element</xs:documentation>
    </xs:annotation>
    <xs:complexType>
      <xs:attributeGroup  ref="memberAttributes"/>
    </xs:complexType>
  </xs:element>

  <xs:element name="members">
    <xs:annotation>
      <xs:documentation>Set of member elements</xs:documentation>
    </xs:annotation>
    <xs:complexType>
      <xs:sequence minOccurs="1" maxOccurs="unbounded">
        <xs:element ref="member"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
Tobias
  • 67
  • 1
  • 2
  • 6

1 Answers1

2

Setting minOccurs="1" is indeed the way to specify that an element is required. (Note that minOccurs="1" is the default anyway, so the effect of omitting the minOccurs attribute is the same as setting it to be 1.) Your XSD should achieve the results you seek.

Confirmation: Your XSD, made to be stand-alone:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:attributeGroup name="memberAttributes">
    <xs:annotation>
      <xs:documentation>Set of attributes for member element</xs:documentation>
    </xs:annotation>
    <xs:attribute name="employeeid" type="xs:string" use="required"/>
    <xs:attribute name="dob" use="required"/>
    <xs:attribute name="gender" use="required"/>
    <xs:attribute name="salary" use="required">
      <xs:simpleType>
        <xs:restriction base="xs:decimal">
          <xs:totalDigits value="13"/>
          <xs:maxInclusive value="99999999999.99"/>
        </xs:restriction>
      </xs:simpleType>
    </xs:attribute>
    <xs:attribute name="postcode" use="required"/>
  </xs:attributeGroup>

  <xs:element name="member">
    <xs:annotation>
      <xs:documentation>Member element</xs:documentation>
    </xs:annotation>
    <xs:complexType>
      <xs:attributeGroup  ref="memberAttributes"/>
    </xs:complexType>
  </xs:element>

  <xs:element name="members">
    <xs:annotation>
      <xs:documentation>Set of member elements</xs:documentation>
    </xs:annotation>
    <xs:complexType>
      <xs:sequence minOccurs="1" maxOccurs="unbounded">
        <xs:element ref="member"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

Will find this XML document to be valid:

<?xml version="1.0" encoding="UTF-8"?>
<members>
  <member employeeid="" dob="" gender="" salary="1.0" postcode=""/>
  <member employeeid="" dob="" gender="" salary="1.0" postcode=""/>
</members>

But will find this XML document to be invalid:

<?xml version="1.0" encoding="UTF-8"?>
<members/>

And will issue an error such as the following:

[Error] try.xml:3:52: cvc-complex-type.2.4.b: The content of element 'members' is not complete. One of '{member}' is expected.

Final note: Placing minOccurs and maxOccurs on the xs:sequence indicates requiredness and cardinality constraints on the entire sequence. In this case, there's only a single member element in the sequence, so the forms are equivalent, but be aware of the distinction when there are more children of the xs:sequence.

kjhughes
  • 106,133
  • 27
  • 181
  • 240