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?
Your XML is valid against your XSD as posted.
If you were to omit the required
DivisionIDelement, you would indeed receive an validation error along the lines ofThis error should be read as saying not that
Unitis required afterproductIDbut rather thatUnitorDivisionIDis expected to followproductID. 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