Unordered elements in XSD with obligatory and unbounded elements?

前端 未结 1 773
栀梦
栀梦 2021-01-22 23:47

I have the following element in my XSD:


    

        
相关标签:
1条回答
  • 2021-01-23 00:29

    Three suggestions. Either:

    1. Impose an ordering. Almost always the perceived need to allow any ordering of elements is unnecessary in practice.
    2. Use XSD 1.1, where maxOccurs="unbounded" is supported on xsd:all.
    3. Use a wrapper around the element you wish to allow to have maxOccurs="unbounded". See additionalList in the XSD below for a working example.

    XSD with wrapper element to work around unbounded xsd:all limitation

    <?xml version="1.0" encoding="utf-8" standalone="no"?>
    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" 
               elementFormDefault="qualified">
      <xs:element name="documents">
        <xs:complexType>
          <xs:all>
            <xs:element name="invoice" minOccurs="1" maxOccurs="1">
              <xs:simpleType>
                <xs:restriction base="xs:string">
                  <xs:minLength value="1"/>
                </xs:restriction>
              </xs:simpleType>
            </xs:element>
            <xs:element name="report" minOccurs="0" maxOccurs="1">
              <xs:simpleType>
                <xs:restriction base="xs:string">
                  <xs:minLength value="1"/>
                </xs:restriction>
              </xs:simpleType>
            </xs:element>
            <xs:element name="additionalList" minOccurs="0">
              <xs:complexType>
                <xs:sequence>
                  <xs:element name="additional" minOccurs="0" maxOccurs="unbounded">
                    <xs:simpleType>
                      <xs:restriction base="xs:string">
                        <xs:minLength value="1"/>
                      </xs:restriction>
                    </xs:simpleType>
                  </xs:element>
                </xs:sequence>
              </xs:complexType>
            </xs:element>
          </xs:all>
        </xs:complexType>
      </xs:element>
    </xs:schema>
    
    0 讨论(0)
提交回复
热议问题