XML document not validating against XSD schema

40 views Asked by At

I have the below XML Schema:

         <xs:element name="_links">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="self">
                <xs:complexType>
                  <xs:sequence>
                    <xs:element minOccurs="0" maxOccurs="unbounded" name="href" type="xs:string" />
                  </xs:sequence>
                </xs:complexType>
              </xs:element>
              <xs:element name="next">
                <xs:complexType>
                  <xs:sequence>
                    <xs:element minOccurs="0" maxOccurs="unbounded" name="href" type="xs:string" />
                  </xs:sequence>
                </xs:complexType>
              </xs:element>
              <xs:element name="prev">
                <xs:complexType>
                  <xs:sequence>
                    <xs:element minOccurs="0" maxOccurs="unbounded" name="href" type="xs:string" />
                  </xs:sequence>
                </xs:complexType>
              </xs:element>
            </xs:sequence>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>

A specific XML document I am validating does not have a "next" field in it (although some documents can). When I load this document I am receiving the error:

Error validating source XML against schema - The element '_links' in namespace 'http://www.orbis-software.com/WebSvcCon' has invalid child element 'prev' in namespace 'http://www.orbis-software.com/WebSvcCon'. List of possible elements expected: 'next' in namespace 'http://www.orbis-software.com/WebSvcCon'.

As per the XSD the "next" element is set to minOccurs=0 making it optional, so why I am receiving the error?

2

There are 2 answers

1
Martin Honnen On

In your schema the contents of the _links elements is defined in

    <xs:element name="_links">
      <xs:complexType>
        <xs:sequence>
          <xs:element name="self">
            <xs:complexType>
              <xs:sequence>
                <xs:element minOccurs="0" maxOccurs="unbounded" name="href" type="xs:string" />
              </xs:sequence>
            </xs:complexType>
          </xs:element>
          <xs:element name="next">
            <xs:complexType>
              <xs:sequence>
                <xs:element minOccurs="0" maxOccurs="unbounded" name="href" type="xs:string" />
              </xs:sequence>
            </xs:complexType>
          </xs:element>
          <xs:element name="prev">
            <xs:complexType>
              <xs:sequence>
                <xs:element minOccurs="0" maxOccurs="unbounded" name="href" type="xs:string" />
              </xs:sequence>
            </xs:complexType>
          </xs:element>
        </xs:sequence>
      </xs:complexType>
    </xs:element>

as a sequence of three elements, namely self, next and prev. None of these elements is optional as none of them has a mixOccurs="0", there is only a mixOccurs="0" on the content of each of those elements as they can can contain 0 to unbounded href elements.

So <_links><self/><next/><prev/></_links> is possible but you can't leave out any of those three elements.

0
Michael On

If anyone is interested I got this working by specifying the min occurs twice:

        <xs:element name="_links">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="self">
                <xs:complexType>
                  <xs:sequence>
                    <xs:element minOccurs="0" maxOccurs="unbounded" name="href" type="xs:string" />
                  </xs:sequence>
                </xs:complexType>
              </xs:element>
              <xs:element minOccurs="0" name="next">
                <xs:complexType>
                  <xs:sequence>
                    <xs:element minOccurs="0" maxOccurs="unbounded" name="href" type="xs:string" />
                  </xs:sequence>
                </xs:complexType>
              </xs:element>
              <xs:element minOccurs="0" name="prev">
                <xs:complexType>
                  <xs:sequence>
                    <xs:element minOccurs="0" maxOccurs="unbounded" name="href" type="xs:string" />
                  </xs:sequence>
                </xs:complexType>
              </xs:element>
            </xs:sequence>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>