Search code examples
xmlxsdxsd-validationxml-validation

Allow sequence OR empty in XSD


Looking to figure out how to match either on a set sequence or no value. Example below of my sequence, but I'm stuck for how to get it to accept either the sequence as written, or the <body> tag existing with no value inside it.

<xs:element name="body">
    <xs:complexType>
        <xs:sequence>
            <xs:element name="id"   type="xs:string"/>
            <xs:element name="name" type="xs:string"/>
            <xs:element name="type" type="xs:string"/>
            <xs:element name="user" type="xs:string"/>
        </xs:sequence>
    </xs:complexType>
</xs:element>

I want the above to match on either a blank (as below) or a correctly filled tag.

<body></body>


Solution

  • You can add a minOccurs="0" to xs:sequence to allow the entire sequence to be optional.

    See also