xsd:choice的minOccurs和maxOccurs的含义



之间有什么区别

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

和:

  <choice maxOccurs="1">
    <element ref="test:A" maxOccurs="unbounded"/>
  </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

<choice minOccurs="0" maxOccurs="1">
  <element name="A" minOccurs="1" maxOccurs="1"/>
  <element name="B" minOccurs="1" maxOccurs="1"/>
</choice>

Regex[AB]?

有效序列包括:

  • 什么都没有
  • A
  • B

<choice minOccurs="1" maxOccurs="unbounded">
  <element name="A" minOccurs="1" maxOccurs="1"/>
  <element name="B" minOccurs="1" maxOccurs="1"/>
</choice>

Regex[AB]+

有效序列包括:

  • 含有至少一个A或一个B的任何组合

<choice minOccurs="1" maxOccurs="1">
  <element name="A" minOccurs="1" maxOccurs="unbounded"/>
  <element name="B" minOccurs="1" maxOccurs="unbounded"/>
</choice>

RegexA+|B+

有效序列包括:

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

<choice minOccurs="1" maxOccurs="1">
  <element name="A" minOccurs="0" maxOccurs="1"/>
  <element name="B" minOccurs="0" maxOccurs="unbounded"/>
</choice>

RegexA?|B*

有效序列包括:

  • 什么都没有
  • A
  • B
  • BB
  • BBB
  • 等等

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

Etc

Regex[AB]*

有效序列包括:

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

默认值

@minOccurs@maxOccurs的默认值均为1。

最新更新