XSD 中的无序元素具有强制性和无界元素? [英] Unordered elements in XSD with obligatory and unbounded elements?

查看:35
本文介绍了XSD 中的无序元素具有强制性和无界元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 XSD 中有以下元素:

I have the following element in my XSD:

<xs:element name="documents" minOccurs="1" maxOccurs="1">
    <xs:complexType>
        <xs:sequence>

            <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="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>

您可以看到文档必须始终带有发票,并且可以选择包含单个报告和零个或多个附加项.

You can see that documents must always have an invoice and optionally it can have a single report and zero or more additionals.

问题是这些元素可以有不同的出现顺序,所以我不能再使用 sequence.我尝试使用 all 但问题是附加元素,因为它有 maxOccurs="unbounded".

The problem is that these elements can have a different order of appearance, so I can´t use a sequence anymore. I tried to use all but then the problem is the additional element, since it has maxOccurs="unbounded".

如何获得一个无序列表的元素,其中一个元素总是需要的,而另一个元素的出现次数不受限制?

How can I have an unordered list of elements with one of those those elements being always required and another element having unlimited occurrences?

推荐答案

三个建议.要么:

  1. 强制排序.几乎总是认为需要允许任何元素的排序在实践中是不必要的.
  2. 使用 XSD 1.1,其中 maxOccurs="unbounded"xsd:all 上受支持.
  3. 在您希望允许的元素周围使用包装器maxOccurs="unbounded".有关工作示例,请参阅下面 XSD 中的 additionalList.
  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 可以解决无界 xsd:all 限制

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

这篇关于XSD 中的无序元素具有强制性和无界元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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