user3501569
user3501569

Reputation: 127

XSD optional element minOccurs="0" but on validation it is expected?

My requirement is I have three elements where ProductID and DivisionID is required and Unit is optional.

XSD

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" 
           attributeFormDefault="unqualified" elementFormDefault="qualified">
<xs:element name="Message">
<xs:complexType>
    <xs:sequence>
        <xs:element name="Product">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="productID">
                    <xs:simpleType>
                            <xs:restriction base="xs:string">
                              <xs:minLength value="1"/>
                              <xs:maxLength value="100"/>
                            </xs:restriction>
                    </xs:simpleType>
                </xs:element>
                <xs:element name="Unit" minOccurs="0">
                    <xs:simpleType>
                            <xs:restriction base="xs:string">
                              <xs:minLength value="0"/>
                              <xs:maxLength value="30"/>
                            </xs:restriction>
                    </xs:simpleType>
                </xs:element>
                <xs:element name="DivisionID">
                    <xs:simpleType>
                            <xs:restriction base="xs:string">
                              <xs:minLength value="0"/>
                              <xs:maxLength value="30"/>
                            </xs:restriction>
                    </xs:simpleType>
                </xs:element>
            </xs:sequence>
        </xs:complexType>
        </xs:element>
    </xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>

XML

<?xml version="1.0" encoding="UTF-8"?>
<Message>
   <Product>
      <productID>ABC-EDI</productID>

   </Product>
</Message>

Validation error

Cvc-complex-type.2.4.b: The Content Of Element 'Product' Is Not Complete. One Of '{Unit, DivisionID}' Is Expected., Line '4', Column '14'.

Question

Error should be that only DivisionID is expected. Why Unit is expected?

Upvotes: 2

Views: 4002

Answers (1)

kjhughes
kjhughes

Reputation: 111521

Your XML is valid against your XSD as posted.

If you were to omit the required DivisionID element, you would indeed receive an validation error along the lines of

cvc-complex-type.2.4.b: The content of element 'Product' is not complete. One of '{Unit, DivisionID}' is expected.

This error should be read as saying not that Unit is required after productID but rather that Unit or DivisionID is expected to follow productID. You're understandably looking to be told the minimum change necessary to meet the XSD's requirements. However, it's making a broader statement along the lines of

Hey, I just saw a close tag for Product, and its content model remains unsatisfied. Before Product ends, I expected to see one of Unit or DivisionID at this point.

Upvotes: 3

Related Questions