How to make an element in XML schema optional?

后端 未结 2 1753
眼角桃花
眼角桃花 2020-12-12 21:51

So I got this XML schema:

 

        
相关标签:
2条回答
  • 2020-12-12 22:17

    Set the minOccurs attribute to 0 in the schema like so:

    <?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" minOccurs="0" />
                            </xs:sequence>
                        </xs:complexType>
                    </xs:element>
                </xs:sequence>
            </xs:complexType>
        </xs:element> </xs:schema>
    
    0 讨论(0)
  • 2020-12-12 22:20

    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.

    0 讨论(0)
提交回复
热议问题