XSD 架构只有一个元素出现多次,而 xs:all [英] XSD schema only one element appears multiple times while xs:all

查看:28
本文介绍了XSD 架构只有一个元素出现多次,而 xs:all的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个如下所示的架构:

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>

现在的要求是我们可以为每个联系人添加多个手机号码,即一个xml,如下所示:

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>

因为 xsd 中的 Contact 元素使用 xs:all,所以不允许添加多个 Mobile 元素.有没有办法允许多个 Mobile 元素但只出现其余元素?名称 &Id 应该只在 xml 中出现一次.

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.

推荐答案

这是可能的,但您必须更改 元素的模型,尤其是关于元素的顺序.

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>

此外, 不允许作为 xs:integer 的限制方面.我以为你的意思是 .

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

这篇关于XSD 架构只有一个元素出现多次,而 xs:all的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆