How to make an element in XML schema optional?

Richard Knop picture Richard Knop · Feb 11, 2012 · Viewed 141.3k times · Source

So I got this XML schema:

<?xml version="1.0"?> <xs:schema version="1.0"
           xmlns:xs="http://www.w3.org/2001/XMLSchema"
           elementFormDefault="qualified">
    <xs:element name="request">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="amenity">
                    <xs:complexType>
                        <xs:sequence>
                            <xs:element name="description" type="xs:string" />
                        </xs:sequence>
                    </xs:complexType>
                </xs:element>
            </xs:sequence>
        </xs:complexType>
    </xs:element> </xs:schema>

How do I make the description element optional? So both XML with the description element and without will validate against the XSD.

Answer

Dmitry Kudryavtsev picture Dmitry Kudryavtsev · Feb 11, 2012

Try this

<xs:element name="description" type="xs:string" minOccurs="0" maxOccurs="1" />

if you want 0 or 1 "description" elements, Or

<xs:element name="description" type="xs:string" minOccurs="0" maxOccurs="unbounded" />

if you want 0 to infinity number of "description" elements.