XSD 中的无序元素具有强制性和无界元素? [英] Unordered elements in XSD with obligatory and unbounded elements?
问题描述
我的 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?
推荐答案
三个建议.要么:
- 强制排序.几乎总是认为需要允许任何元素的排序在实践中是不必要的.
- 使用 XSD 1.1,其中
maxOccurs="unbounded"
在xsd:all
上受支持. - 在您希望允许的元素周围使用包装器
maxOccurs="unbounded"
.有关工作示例,请参阅下面 XSD 中的additionalList
.
- Impose an ordering. Almost always the perceived need to allow any ordering of elements is unnecessary in practice.
- Use XSD 1.1, where
maxOccurs="unbounded"
is supported onxsd:all
. - Use a wrapper around the element you wish to allow to have
maxOccurs="unbounded"
. SeeadditionalList
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屋!