“这些元素中的任何一个或多个,但必须至少是一个”的 XML 模式构造

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/100228/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 12:03:10  来源:igfitidea点击:

XML schema construct for "any one or more of these elements but must be at least one"

xmlxsdschema

提问by Scott Leis

I'm trying to set up part of a schema that's like a "Sequence" where all child elements are optional, but at least one of the elements mustbe present, and there could be more than one of them.

我正在尝试设置类似于“序列”的模式的一部分,其中所有子元素都是可选的,但至少必须存在一个元素,并且可能有多个。

I tried doing the following, but XMLSpy complains that "The content model contains the elements <element name="DateConstant"> and <element name="DateConstant"> which cannot be uniquely determined.":

我尝试执行以下操作,但 XMLSpy 抱怨“内容模型包含无法唯一确定的元素 <element name="DateConstant"> 和 <element name="DateConstant">。”:

    <xs:choice>
        <xs:sequence>
            <xs:element name="DateConstant"/>
            <xs:element name="TimeConstant"/>
        </xs:sequence>
        <xs:element name="DateConstant"/>
        <xs:element name="TimeConstant"/>
    </xs:choice>

Can this be done (and if so, how)?

这可以完成吗(如果可以,如何完成)?

Some clarification: I only want to allow one of each element of the same name. There can be one "DateConstant" and/or one "TimeConstant", but not two of either. Gizmo's answer matches my requirements, but it's impractical for a larger number of elements. Hurst's answer allows two or more elements of the same name, which I don't want.

一些澄清:我只想允许同名的每个元素之一。可以有一个“DateConstant”和/或一个“TimeConstant”,但不能有两个。Gizmo 的答案符合我的要求,但对于大量元素来说是不切实际的。Hurst 的回答允许两个或多个同名元素,这是我不想要的。

回答by gizmo

Try this:

尝试这个:

<xs:choice>
  <xs:sequence>
    <xs:element name="Elem1" />
    <xs:element name="Elem2" minOccurs="0" />
    <xs:element name="Elem3" minOccurs="0" />
  </xs:sequence>
  <xs:sequence>
    <xs:element name="Elem2" />
    <xs:element name="Elem3" minOccurs="0" />
  </xs:sequence>
  <xs:element name="Elem3" />
</xs:choice>

Doing so, you force either to choose the first element and then the rest is optional, either the second element and the rest is optional, either the third element.

这样做,您强制要么选择第一个元素,然后选择其余元素,要么选择第二个元素,其余元素可选,要么选择第三个元素。

This should do what you want, I hope.

这应该做你想做的,我希望。

Of course, you could place the sub-sequences into groups, to avoid to duplicate an element in each sequence if you realize you miss one.

当然,您可以将子序列分组,以避免在发现丢失一个元素时在每个序列中复制一个元素。

回答by hurst

According to the technical article on MSDN titled Understanding XML Schemaat http://msdn.microsoft.com/en-us/library/aa468557.aspx#understandxsd_topic5you can take advantage of constraints such as minOccurson the choice definition (compositor) itself:

根据 位于http://msdn.microsoft.com/en-us/library/aa468557.aspx#understandxsd_topic5 的MSDN 上题为“理解 XML 架构”的技术文章, 您可以利用诸如minOccurs之类的约束选择定义(合成器)本身:

"Using occurrence constraints on a compositor applies to the entire group as a whole"

“对合成器使用出现约束适用于整个组作为一个整体”

(See the more sophisticated example that uses nested complex types and the AuthorType example)

(请参阅使用嵌套复杂类型的更复杂的示例和 AuthorType 示例)

You stated your requirement as "at least one of the elements must be present, and there could be more than one of them". Thus, I propose you try the following:

您将您的要求表述为“至少必须存在一个元素,并且可以有多个元素”。因此,我建议您尝试以下操作:

<xs:choice minOccurs="1" maxOccurs="unbounded">
    <xs:element name="DateConstant" type="..."/>
    <xs:element name="TimeConstant" type="..."/>
</xs:choice>

回答by Enigmatic

@hurst,

@赫斯特,

Unfortunately you have failed to understand the original question. Placing minOccurs="1" on the choice is satisfied automatically when ALL elements that have minOccurs="0" are contained as options.

不幸的是,您未能理解最初的问题。当包含 minOccurs="0" 的所有元素作为选项包含时,将自动满足将 minOccurs="1" 放在选项上。

Thus you have failed to account for the "at least one" required by the original poster, because no elements correctly satisfies 1 occurrance of two completely optional elements.

因此,您没有考虑原始海报要求的“至少一个”,因为没有元素正确满足两个完全可选元素的 1 次出现。

So far I am unable to find a solution to this as minOccur/maxOccur both relate to the group in which they are defined and DO NOT relate to an overall number of nodes. Nor can you use the choice element to define the same named element more than once or it becomes "ambiguous". I have seen some examples use references instead of elements of a specific type, but I believe this fails the microsoft XSD parser.

到目前为止,我无法找到解决方案,因为 minOccur/maxOccur 都与定义它们的组有关,而与节点总数无关。您也不能使用选择元素多次定义同一个命名元素,否则它会变得“模棱两可”。我已经看到一些示例使用引用而不是特定类型的元素,但我相信这会使 microsoft XSD 解析器失败。

<xs:choice minOccurs="1" maxOccurs="1">
  <xs:sequence minOccurs="1" maxOccurs="1">
    <xs:element name="Elem1" minOccurs="1" maxOccurs="1" />
    <xs:element name="Elem2" minOccurs="0" maxOccurs="1" />
  </xs:sequence>
  <xs:sequence >
    <xs:element name="Elem2" minOccurs="1" maxOccurs="1" />
  </xs:sequence>
</xs:choice>

Here you can see that either you have the first sequence (which MUST have Elem1 but may have Elem2 optionally), or you have the second sequence (which MUST have Elem2).

在这里你可以看到,要么你有第一个序列(必须有 Elem1 但可以有 Elem2 可选),或者你有第二个序列(必须有 Elem2)。

Hence you now have "any one or more" of these 2 elements. Of course this gets exponentially more complex the more options you have as you need to provide additional choices for all possible combinations.

因此,您现在拥有这两个元素中的“任何一个或多个”。当然,这会变得更加复杂,因为您需要为所有可能的组合提供更多选择,因此您拥有的选项越多。