XSD 中的 xs:choice 是否允许结果 XML 中有多个元素? [英] Does xs:choice in XSD allows multiple elements in resulting XML?

查看:38
本文介绍了XSD 中的 xs:choice 是否允许结果 XML 中有多个元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我被 XSD 的选择"元素描述困住了.它指出,在有效的 XML 中只能找到该标签内的元素中的一个元素:

I am stuck with XSD's 'choice' element description. It states, that only one element, from those found inside that tag can be found in valid XML:

允许一个且仅一个包含在所选元素中的元素组存在于包含元素中.

Allows one and only one of the elements contained in the selected group to be present within the containing element.

(http://msdn.microsoft.com/en-us/library/ms256109%28v=vs.110%29.aspx)

另一方面,看起来属性 ma​​xOccurs="unbounded" 可能会将情况改变为相反的情况,现在任意数量的元素都可能出现在用 'choice' 描述的类型中:请参阅这个问题:

From the other hand, looks like attribute maxOccurs="unbounded" may change the situation to the opposite, and now any number of elements may appear inside the type desribed with 'choice': please refer to this question:

XSD - 如何允许元素以任意顺序任意次数?

那么在里面有选择指示符的类型中可以找到多少个元素?

So how many elements can be found inside the type which has a choice indicator inside?

推荐答案

MSDN 页面上说选择允许一个且仅一个包含在所选组中的元素"措辞不当.嗯,不,那太慷慨了.作为 xsd:choice 的描述,它是完全错误的.

The MSDN page that says choice allows "one and only one of the elements contained in the selected group" is badly phrased. Well, no, that's too generous. As a description of xsd:choice, it's flat wrong.

一个选择包含零个或多个备选方案,并且当且仅当这些备选方案中至少有一个与输入匹配时才匹配输入的一部分.(实际上,这几乎总是意味着如果这些备选方案中的一个恰好与输入相匹配":如果有多个备选方案与输入相匹配,则内容模型几乎肯定违反了唯一粒子归因"规则.)

A choice contains zero or more alternatives, and matches part of the input if and only if at least one of those alternatives matches the input. (In practice, this almost always means 'if exactly one of those alternatives matches the input': if more than one alternative matches the input, the content model almost certainly violates the 'Unique Particle Attribution' rule.)

因此,在 A1、A2、A3、... An 之间进行选择,当且仅当 A1 与该序列匹配,或者 A2、A3、... 或 An 匹配输入中的元素序列.

So a choice among alternatives A1, A2, A3, ... An matches a sequence of elements in the input if and only if A1 matches that sequence, or A2, or A3, ..., or An.

如果每个备选 A1, ..., An 都是 minOccurs = maxOccurs = 1 的单个元素,那么它们都不可重复,如果选择本身不可重复,那么您引用的描述或多或少是准确的.但是有几种情况中的任何一种都可能导致多个元素被一个选择匹配:

If each alternative A1, ..., An is a single element with minOccurs = maxOccurs = 1, so none of them are repeatable, and if the choice itself is not repeatable, then the description you quote is more or less accurate. But any of several things can result in multiple elements being matched by a single choice:

  • 选择本身可能是可重复的(正如 lexicore 已经指出的那样);以下选项与 A、B 和 C 元素的任意序列匹配:

  • The choice may itself be repeatable (as already pointed out by lexicore); the following choice matches any sequence of A, B, and C elements:

<xsd:choice minOccurs="0" maxOccurs="unbounded">
  <xsd:element ref="A"/>
  <xsd:element ref="B"/>
  <xsd:element ref="C"/>
</xsd:choice>

  • 个别替代方案可能是可重复的;以下选择匹配任何 A 元素的非空序列,或任何 B 元素的非空序列,或恰好一个 C 元素:

  • The individual alternatives may be repeatable; the following choice matches any non-empty sequence of A elements, or any non-empty sequence of B elements, or exactly one C element:

    <xsd:choice>
      <xsd:element ref="A" maxOccurs="unbounded"/>
      <xsd:element ref="B" maxOccurs="unbounded"/>
      <xsd:element ref="C"/>
    </xsd:choice>
    

  • 单独的选择可能是匹配多个元素的序列或选择;以下选项匹配任何非空的 A 和 B 元素序列,或序列 D、E、F:

  • The individual alternatives may be sequences or choices that match more than one element; the following choice matches any non-empty sequence of A and B elements, or the sequence D, E, F:

    <xsd:choice>
      <xsd:choice maxOccurs="unbounded">
        <xsd:element ref="A"/>
        <xsd:element ref="B"/>
      </xsd:choice>
      <xsd:sequence>
         <xsd:element ref="D"/>
         <xsd:element ref="E"/>
         <xsd:element ref="F"/>
      </xsd:sequence>
    </xsd:choice>
    

  • 问题的答案

    那么在里面有选择指示符的类型中可以找到多少个元素?

    So how many elements can be found inside the type which has a choice indicator inside?

    与问题的答案相同

    那么在一个元素中可以找到多少个元素?

    So how many elements can be found inside an element?

    那么匹配正则表达式的符号序列有多长?

    So how long is a sequence of symbols which matches a regular expression?

    这篇关于XSD 中的 xs:choice 是否允许结果 XML 中有多个元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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