“这些元素中的任何一个或多个,但必须至少是一个"的 XML 模式构造 [英] XML schema construct for "any one or more of these elements but must be at least one"

查看:14
本文介绍了“这些元素中的任何一个或多个,但必须至少是一个"的 XML 模式构造的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试设置类似于序列"的架构的一部分,其中所有子元素都是可选的,但至少有一个元素必须存在,并且可能还有更多比其中之一.

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 must be present, and there could be more than one of them.

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

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.":

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

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

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.

推荐答案

试试这个:

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

这篇关于“这些元素中的任何一个或多个,但必须至少是一个"的 XML 模式构造的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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