XSD element required despite minOccurs=0

12,957

Global declarations cannot contain the attributes minOccurs, maxOccurs, or use. You might want to refer this link. http://www.w3.org/TR/xmlschema-0/#OccurrenceConstraints

Share:
12,957
treeNinja
Author by

treeNinja

Updated on June 04, 2022

Comments

  • treeNinja
    treeNinja almost 2 years

    I am new to XSD and confused at current behavior and I am unsure what I am missing. In building a WCF service in VB I am trying to creating some elements that are optional. I have 2 elements defined that I want to be optional, either they are part of the webservice request or not, I don't care. However I keep getting errors that both are required. Am I missing something obvious? Sequence and All both should be ok with minOccurrs, as well as references.

    In my research I know I am to use the minOccurs="0" As cited below from w3Schools

    MinOccurs defined...

    The "minOccurs" indicator specifies the minimum number of times an element can occur.

    <xs:element name="person">
       <xs:complexType>
         <xs:sequence>
           <xs:element name="full_name" type="xs:string"/>
           <xs:element name="child_name" type="xs:string"
           maxOccurs="10" minOccurs="0"/>
         </xs:sequence>
       </xs:complexType>
     </xs:element> 
    

    The example above indicates that the "child_name" element can occur a minimum of zero times and a maximum of ten times in the "person" element.

    With this I have defined my ThirdPartyMessage as follows: With the goal of having ExternalID and SecondID (no control on the name)

    <xs:element name="ThirdPartyMessage">
        <xs:complexType>
          <xs:all>
            <xs:element ref="Subject"/>
            <xs:element ref="Message"/>
            <xs:element ref="RequestType" />
            <xs:element ref="ExternalID" minOccurs="0"/>
            <xs:element ref="SecondID" minOccurs="0"/>
          </xs:all>
        </xs:complexType>
      </xs:element>
    

    As well as within

      <xs:complexType name="MessageHeaderType">
        <xs:sequence>
          <xs:element name="RqUID" type="UUID"/>
          <xs:element name="AsyncRqUID" type="UUID" minOccurs="0"/>
          <xs:element name="PartnerKey" type="Identifier" minOccurs="0"/>
          <xs:element ref="ExternalID" minOccurs="0" maxOccurs="1"/>
          <xs:element ref="SecondID" minOccurs="0" maxOccurs="1"/>
        </xs:sequence>
      </xs:complexType>
    

    Defined as

      <xs:element name="ExternalID">
        <xs:simpleType>
          <xs:restriction base="xs:string">
            <xs:minLength value="0"/>
            <xs:maxLength value="15"/>
          </xs:restriction>
        </xs:simpleType>
      </xs:element>
    
      <xs:element name="SecondID">
        <xs:simpleType>
          <xs:restriction base="Integer">
          </xs:restriction>
        </xs:simpleType>
      </xs:element>