-->

XSD schema only one element appears multiple times

2019-08-06 13:41发布

问题:

I have a schema which looks like:

<xs:schema attributeFormDefault="qualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="Contacts">
    <xs:complexType>
        <xs:sequence>
            <xs:element name="Contact" maxOccurs="unbounded">
                <xs:complexType>
                    <xs:all>
                        <xs:element name="Id">
                            <xs:simpleType>
                                <xs:restriction base="xs:integer">
                                  <xs:minLength value="1"/>
                                </xs:restriction>
                            </xs:simpleType>
                        </xs:element>
                        <xs:element name="Name">
                            <xs:simpleType>
                                <xs:restriction base="xs:token">
                                    <xs:pattern
                                    value="[a-z\-_]+"
                                    />
                                  </xs:restriction>
                            </xs:simpleType>
                        </xs:element>
                        <xs:element name="Mobile">
                            <xs:simpleType>
                                <xs:restriction base="xs:Integer">
                                    <xs:minLength value="1"/>
                                  </xs:restriction>
                            </xs:simpleType>
                        </xs:element>
                    </xs:all>
                </xs:complexType>
            </xs:element>
        </xs:sequence>
    </xs:complexType>
 </xs:element>

Now the requirement is that we can add multiple Mobile phone numbers for each contact i.e. a xml as follows:

<?xml version="1.0" encoding="UTF-8"?>
<Contacts>
  <Contact>
    <Id>1</Id>
    <Name>Rebecca</Name>
    <Mobile>1234</Mobile>
    <Mobile>4567</Mobile>
  </Contact>
</Contacts>

Because the Contact element in xsd uses xs:all, it doesn't allow adding multiple Mobile elements. Is there a way to allow multiple Mobile elements but only single occurrence of rest of the elements? Name & Id should have one occurrence only in xml.

回答1:

It is possible but you have to change the model of the <Contact> element, especially regarding the order of the elements.

If you are sure that <Id> comes first, followed by <Name>, then at least one <Mobile>, then this can be a solution:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="qualified">
  <xs:element name="Contacts">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="Contact" maxOccurs="unbounded">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="Id">
                <xs:simpleType>
                  <xs:restriction base="xs:integer">
                    <xs:minInclusive value="1"/>
                  </xs:restriction>
                </xs:simpleType>
              </xs:element>
              <xs:element name="Name">
                <xs:simpleType>
                  <xs:restriction base="xs:token">
                    <xs:pattern value="[A-Za-z\-_]+"/>
                  </xs:restriction>
                </xs:simpleType>
              </xs:element>
              <xs:element name="Mobile" maxOccurs="unbounded">
                <xs:simpleType>
                  <xs:restriction base="xs:integer">
                    <xs:minInclusive value="1"/>
                  </xs:restriction>
                </xs:simpleType>
              </xs:element>
            </xs:sequence>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

Also, the <xs:minLength> is not allowed as a restriction facet for xs:integer. I assumed you meant <xs:minInclusive>.