XSD schema only one element appears multiple times while xs:all

China☆狼群 提交于 2019-12-14 03:58:37

问题


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>.



来源:https://stackoverflow.com/questions/28068303/xsd-schema-only-one-element-appears-multiple-times-while-xsall

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!