Xml xsd:choice的minOccurs和maxOccurs的含义?

Xml xsd:choice的minOccurs和maxOccurs的含义?,xml,xsd,xsd-validation,xml-validation,Xml,Xsd,Xsd Validation,Xml Validation,以下两者之间的区别是什么: <choice maxOccurs="unbounded"> <element ref="test:A" maxOccurs="1"/> </choice> 以及: 出于任何实际目的?在这种情况下,什么都没有,但当您为选择添加替代方案时,差异就会显现出来: <choice maxOccurs="unbounded"> <element ref="test:A" maxOccurs="

以下两者之间的区别是什么:

  <choice maxOccurs="unbounded">
     <element ref="test:A" maxOccurs="1"/>
  </choice>

以及:



出于任何实际目的?

在这种情况下,什么都没有,但当您为选择添加替代方案时,差异就会显现出来:

<choice maxOccurs="unbounded">
  <element ref="test:A" maxOccurs="1"/>
  <element ref="test:B" maxOccurs="1"/>
</choice>

允许任意数量的A和B元素以任意顺序排列,而

<choice maxOccurs="1">
  <element ref="test:A" maxOccurs="unbounded"/>
  <element ref="test:B" maxOccurs="unbounded"/>
</choice>


允许任意数量的As或任意数量的Bs,但不允许两者的混合。

该特定组合没有区别。选择单个备选方案无限次与选择一次允许单个备选方案无限次相同

如何思考
xsd:choice
基数 当
@minOccurs
@maxOccurs
出现在
xs:choice
上时,备选方案中的最小或最大选择次数受到限制

然后对于每一个这样的选择,所选择的子选项的基数开始发挥作用

示例组合 下面是一些用正则表达式表示法表示的示例。还提供了给定组合的有效序列示例

<choice minOccurs="1" maxOccurs="1">
  <element name="A" minOccurs="1" maxOccurs="1"/>
  <element name="B" minOccurs="1" maxOccurs="1"/>
</choice>
Regex
[AB]?

有效序列包括:

  • 没什么
  • A
  • B

Regex
A+| B+

有效序列包括:

  • A
  • AA
  • AAA
  • B
  • BB
  • BBB



Regex
[AB]*

有效序列包括:

  • 没什么
  • 至少包含一个A或一个B的任何组合

默认值
@minOccurs
@maxOccurs
的默认值都是1。

实际上,这个问题对于介于1和无界之间的任何maxOccurs值都是有效的,非常好。我想补充一点:可重复的选择和序列通常会导致像JAXB这样的工具出现问题。所以我通常建议不要在模式中使用它们。更正。谢谢,@IanRoberts!谢谢,这证实了我的理解。但是为什么我会从这个限制中得到一个错误:
(很抱歉格式化)。我得到一个org.xml.sax.SAXParseException,用于此“类型“cxT2”的错误。该类型的粒子不是基粒子的有效限制。受限制类型接受的语言包含在原始类型接受的较大语言中(cxT2中的{1,2}在cxT1中的[a]+中接受)
<choice minOccurs="1" maxOccurs="1">
  <element name="A" minOccurs="1" maxOccurs="1"/>
  <element name="B" minOccurs="1" maxOccurs="1"/>
</choice>
<choice minOccurs="0" maxOccurs="1">
  <element name="A" minOccurs="1" maxOccurs="1"/>
  <element name="B" minOccurs="1" maxOccurs="1"/>
</choice>
<choice minOccurs="1" maxOccurs="unbounded">
  <element name="A" minOccurs="1" maxOccurs="1"/>
  <element name="B" minOccurs="1" maxOccurs="1"/>
</choice>
<choice minOccurs="1" maxOccurs="1">
  <element name="A" minOccurs="1" maxOccurs="unbounded"/>
  <element name="B" minOccurs="1" maxOccurs="unbounded"/>
</choice>
<choice minOccurs="1" maxOccurs="1">
  <element name="A" minOccurs="0" maxOccurs="1"/>
  <element name="B" minOccurs="0" maxOccurs="unbounded"/>
</choice>
<choice minOccurs="0" maxOccurs="unbounded">
  <element name="A" minOccurs="1" maxOccurs="1"/>
  <element name="B" minOccurs="1" maxOccurs="1"/>
</choice>
<choice minOccurs="0" maxOccurs="unbounded">
  <element name="A" minOccurs="0" maxOccurs="unbounded"/>
  <element name="B" minOccurs="0" maxOccurs="unbounded"/>
</choice>
<choice minOccurs="0" maxOccurs="unbounded">
  <element name="A" minOccurs="1" maxOccurs="1"/>
  <element name="B" minOccurs="1" maxOccurs="unbounded"/>
</choice>