Search code examples
c#xmlxsdxsd2code

What is the maximum number of elements in an XML document if the XSD does not specify maxOccurs?


I have an XSD schema that defines multiple elements in a document. Two sets of the elements I expect to be collections. One set of elements is defined like follow:

<xsd:element name="Prospects" minOccurs="0">
<xsd:complexType>
  <xsd:sequence>
    <xsd:element name="ROW" minOccurs="0" maxOccurs="unbounded">
      <xsd:complexType>
        <xsd:sequence>
          <xsd:element name="ID" type="xdv:guidKey" nillable="false" />
          <xsd:element name="Name" minOccurs="0">
            <xsd:complexType>
              <xsd:simpleContent>
                <xsd:extension base="xdv:stringLen50">
                  <xsd:attribute name="origVal" type="xdv:stringLen50" use="optional" />
                </xsd:extension>
              </xsd:simpleContent>
            </xsd:complexType>
          </xsd:element>
          ... more stuff...
    </xsd:element>
  </xsd:sequence>
  <xsd:attribute name="name" type="xsd:string" use="required" fixed="Prospects" />
  <xsd:attribute name="alias" type="xsd:string" use="required" fixed="Prospects" />
  <xsd:attribute name="keys" type="xsd:string" use="required" fixed="ProposalID" />
  <xsd:attribute name="codeTableColVal" type="xdv:codeTableColVal" use="optional" />
</xsd:complexType>

The other set of elements looks like this:

<xsd:element name="Employees" minOccurs="0" maxOccurs="1">
<xsd:complexType>
  <xsd:sequence>
    <xsd:element name="ROW">
      <xsd:complexType>
        <xsd:sequence>
          <xsd:element name="ID" type="xdv:guidKey" nillable="false" />
          <xsd:element name="Seq" type="xdv:guidKey" nillable="false" />
          <xsd:element name="CompanyName" minOccurs="0">
            <xsd:complexType>
              <xsd:simpleContent>
                <xsd:extension base="xdv:stringLen32">
                  <xsd:attribute name="origVal" type="xdv:stringLen32" use="optional" />
                </xsd:extension>
              </xsd:simpleContent>
            </xsd:complexType>
          </xsd:element>
          ... more stuff...
    </xsd:element>
  </xsd:sequence>
  <xsd:attribute name="name" type="xsd:string" use="required" fixed="Employees" />
  <xsd:attribute name="alias" type="xsd:string" use="required" fixed="Employees" />
  <xsd:attribute name="keys" type="xsd:string" use="required" fixed="OpportunityID,Seq" />
  <xsd:attribute name="codeTableColVal" type="xdv:codeTableColVal" use="optional" />
</xsd:complexType>

The primary difference being that the former specifies minOccurs="0" for Prospects and no max occurs, then for ROW it defins minOccurs=0 and maxOccurs=unbounded.

For the latter it defines minOccurs=0 and maxOccurs=1 for Employees and for the ROW it does not define a minOccurs or maxOccurs.

When I run the utility program, Xsd2Code, and it generates my C# code, for the Prospects, it creates a Prospects property with a ROWs collection (As a List()) but for the Employees it create an Employee property with a ROW property, not a collection.

My question: whats is the schema rule for this? Since there is no maxOccurs defined on ROW for employee does the min and max for the parent apply or should it be a collection?

I am trying to determine if the utility that is creating my code is wrong or if the .xsd file is wrong.


Solution

  • The default if maxOccurs is not specified is the same as maxOccurs = "1".

    From XML Schema Indicators.

    Occurrence indicators are used to define how often an element can occur.

    Note: For all "Order" and "Group" indicators (any, all, choice, sequence, group name, and group reference) the default value for maxOccurs and minOccurs is 1.