1

Given a valid XSD:

<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="personinfo">
  <xs:complexType>
    <xs:sequence maxOccurs="2">
      <xs:element name="firstname" type="xs:string" minOccurs="0" maxOccurs="1"/>
      <xs:element name="lastname" type="xs:string"/>
    </xs:sequence>
  </xs:complexType>
</xs:element>
</xs:schema>

For a valid XML against this XSD, a <personinfo> should contain max 2 sequence of <firstname> followed by <lastname>. I add minOccurs and maxOccurs to firstname. For an XML file below:

<personinfo>
  <firstname>lalal</firstname>
  <lastname>fwfw</lastname>
  <firstname>lalal</firstname>
  <lastname>fwfw</lastname>
</personinfo>

Why is it still valid by validator? Don't minOccurs and maxOccurs just overwrite the maxOccurs by xs:sequence?

kjhughes
  • 106,133
  • 27
  • 181
  • 240
JACK M
  • 2,627
  • 3
  • 25
  • 43

1 Answers1

0

Don't minOccurs and maxOccurs just overwrite the maxOccurs by xs:sequence?

No, the occurrence constraints on the enclosing xs:sequence mean that the group itself, collectively, can together appear or repeat as specified. The occurrence constraints on the containing individual elements individually affect those elements independent of how the collection of them may occur.

For your XSD, there can be one or two sequences, each with firstname (optional) and lastname (required).

kjhughes
  • 106,133
  • 27
  • 181
  • 240