简体   繁体   中英

XSD - Making elements optional and mandatory in an XML schema

The schema I have generated for an XML looks like below:

 <xs:complexType name="ComplexElement1">
        <xs:sequence>
            <xs:element name="ComplexType1" type="ComplexType1">
            </xs:element>
            <xs:element name="ComplexType2" type="ComplexType2" minOccurs="0">
            </xs:element>
            <xs:element name="ComplexType3">
                <xs:complexType>
                    <xs:sequence maxOccurs="unbounded">
                        <xs:element name="ComplexType4" type="ComplexType4"/>
                    </xs:sequence>
                </xs:complexType>
            </xs:element>
            <xs:element name="ComplexType5" type="ComplexType5" minOccurs="0"/>
            <xs:element name="ComplexType6" type="ComplexType6" minOccurs="0" maxOccurs="1"/>
            <xs:element name="SimpleType1" type="xs:string" minOccurs="0" maxOccurs="1"/>
            <xs:element name="ComplexType7" type="ComplexType7" minOccurs="0" maxOccurs="1"/>
            <xs:element name="SimpleType2" minOccurs="1" maxOccurs="1" type="xs:string"/>
        </xs:sequence>
        <xs:attribute name="Attribute1" type="StringLength3WithNoSpace" use="required"/>
        <xs:attribute name="Attribute2" type="ComplexAttribute1"/>
        <xs:attribute name="Attribute3" type="xs:boolean" use="optional"/>
    </xs:complexType>

As you can see from the XSD, ComplexType 5,6 and 7, and SimpleType 1 are all optional elements . Also, SimpleType2 is a mandatory element.

When I try a request where I don't have SimpleType2 as part of the request, I expect a message similar to

Message validation failed. Errors: [cvc-complex-type.2.4.b: The content of element 'ComplexElement1' is not complete. One of '{"somePath":SimpleType2}' is expected.]

which is working fine.

However, In cases where I dont put any of the optional elements, ComplexType 5, 6 and 7, and SimpleType 1, as well as the mandatory SimpleType2, there's an issue.

The message I expect is the same one as above - since all the other elements are made optional in the sequence using minOccurs="0"

However, the actual error message I receive is :

Message validation failed. Errors: [cvc-complex-type.2.4.b: The content of element 'ComplexElement1' is not complete. One of '{"somePath":ComplexType5, "somePath":ComplexType6, "somePath":SimpleType1, "somePath":ComplexType7, "somePath":SimpleType2}' is expected.]<

which is not really correct, since my schema defines only SimpleType2 to be mandatory and all others to be optional.

Any ideas on what I can do to fix this?

The schema validator looks at each element and decides whether that element is allowed in the current position. If it finds an element that isn't allowed, or finds the end of the sequence where the end of the sequence isn't allowed, then it gives you a list of elements that could validly appear at the current position. That's the way it's designed to work and you're not going to be able to change it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM