简体   繁体   English

定义 XML 架构时,“选择”“组”元素是否有效(XSD)

[英]Is it valid to have a 'choice' of 'group' elements when defining an XML Schema (XSD)

Is it valid to have a 'choice' of 'group' elements when defining an XML Schema (XSD)定义 XML 架构时,“选择”“组”元素是否有效(XSD)

ie is the following valid即以下有效

<xs:complexType name="HeaderType">
  <xs:sequence>
    <xs:element name="reservation-number" type="ReservationNumberType" minOccurs="1" maxOccurs="1" nillable="false" />
    <xs:choice minOccurs="1" maxOccurs="1">
      <xs:group ref="ReservationGroup" />
      <xs:group ref="CancellationGroup"/>
    </xs:choice>
  </xs:sequence>
</xs:complexType>

Where an XML message can represent, for example, either a new reservation or a cancellation of an existing reservation.例如,XML 消息可以表示新预订或取消现有预订。

If the message is for a reservation, then it must include all the elements defined in the ReservationGroup group.如果消息用于预订,则它必须包含 ReservationGroup 组中定义的所有元素。

If it is a cancellation, then it must include all the elements defined in the CancellationGroup group.如果是取消,则它必须包括 CancellationGroup 组中定义的所有元素。

For some reason, my XML editor (Eclipse) does not like this, but does not indicate why.出于某种原因,我的 XML 编辑器(Eclipse)不喜欢这样,但没有说明原因。 It shows there being an error on the line <xs:complexType name="HeaderType"> but does not say what the error is它显示 <xs:complexType name="HeaderType"> 行有错误,但没有说明错误是什么

I'm no XML expert, although I use it quite a lot. 我不是XML专家,虽然我使用它很多。 This isn't the way I'd generally do this sort of structure. 这不是我通常做这种结构的方式。 I would prefer a separate complex types rather than a choice of two groups (see the very end of this answer). 我更喜欢单独的复杂类型而不是两组的选择(参见本答案的最后部分)。

I suspect that the problem is that ReservationGroup and CancellationGroup start with the same element, in which case you will violate the Schema Component Constraint: Unique Particle Attribution (below). 我怀疑问题是ReservationGroup和CancellationGroup以相同的元素开头,在这种情况下你将违反Schema Component Constraint:Unique Particle Attribution(下面)。

http://www.w3.org/TR/2004/REC-xmlschema-1-20041028/#cos-nonambig http://www.w3.org/TR/2004/REC-xmlschema-1-20041028/#cos-nonambig

Schema Component Constraint: Unique Particle Attribution 模式组件约束:独特的粒子归因

A content model must be formed such that during ·validation· of an element information item sequence, the particle component contained directly, indirectly or ·implicitly· therein with which to attempt to ·validate· each item in the sequence in turn can be uniquely determined without examining the content or attributes of that item, and without any information about the items in the remainder of the sequence. 必须形成内容模型,使得在元素信息项序列的验证期间,可以直接地,间接地或者隐含地包含在其中的粒子组件,其中可以依次确定序列中的每个项目。不检查该项目的内容或属性,也没有关于序列其余部分中的项目的任何信息。

Note: This constraint reconstructs for XML Schema the equivalent constraints of [XML 1.0 (Second Edition)] and SGML. 注意:此约束为XML Schema重建[XML 1.0(第二版)]和SGML的等效约束。 Given the presence of element substitution groups and wildcards, the concise expression of this constraint is difficult, see Analysis of the Unique Particle Attribution Constraint (non-normative) (§H) for further discussion. 鉴于元素替换组和通配符的存在,此约束的简明表达很难,请参阅独特粒子归因约束(非规范)(§H)的分析以供进一步讨论。

For example, the two groups below are illegal in the same choice, because each of their first element is "name" which means that you cannot identify which group you are looking at. 例如,下面的两个组在同一个选项中是非法的,因为它们的第一个元素是“name”,这意味着您无法识别您正在查看的组。 However is the first element of ReservationGroup is different from Cancellation group (resDate and cancDate maybe), then the that is valid. 但是,ReservationGroup的第一个元素与Cancellation组(resDate和cancDate可能)不同,那么它是有效的。

Edit: I'd never come across this sort of problem before, and I think its fascinating that the definitions of the groups are totally legal, but if you put them together in a choice, that choice becomes illegal because of the definition of each group. 编辑:我之前从未遇到过这类问题,我认为这些组的定义完全合法是令人着迷的,但如果你把它们放在一个选择中,那么由于每个组的定义,这种选择变得非法。

Groups that cannot form a legal choice 无法形成合法选择的群组

<xs:group name="ReservationGroup">
    <xs:sequence>
        <xs:element name="date"/>
        <xs:element name="name"/>
        <xs:element name="address"/>
    </xs:sequence>
</xs:group>

<xs:group name="CancellationGroup">
    <xs:sequence>
        <xs:element name="date"/>
        <xs:element name="name"/>
        <xs:element name="address"/>
    </xs:sequence>
</xs:group>

Groups that can form a legal choice 可以形成合法选择的团体

<xs:group name="ReservationGroup">
    <xs:sequence>
        <xs:element name="resDate"/>
        <xs:element name="name"/>
        <xs:element name="address"/>
    </xs:sequence>
</xs:group>

<xs:group name="CancellationGroup">
    <xs:sequence>
        <xs:element name="cancDate"/>
        <xs:element name="name"/>
        <xs:element name="address"/>
    </xs:sequence>
</xs:group>

As I mentioned above, I'd do this sort of thing with complex types. 正如我上面提到的,我会用复杂类型做这类事情。 Yes, it adds another element, but it seems the obvious way and I like obviousness. 是的,它增加了另一个元素,但它似乎是显而易见的方式,我喜欢显而易见。

<xs:complexType name="HeaderType">
  <xs:sequence>
    <xs:element name="reservation-number" type="ReservationNumberType" minOccurs="1" maxOccurs="1" nillable="false" />
    <xs:choice minOccurs="1" maxOccurs="1">
      <xs:element name="reservation" type="ReservationType" />
      <xs:element name="cancellation" type="CancellationType" />
    </xs:choice>
  </xs:sequence>
</xs:complexType>

Yes. 是。 It was because both the ReservationGroup and the CancellationGroup had the same first element - a 'reservation-type' element with a fixed value of 'Reservation' in the ReservationGroup and 'Cancellation' in the Cancellationgroup respectively. 这是因为ReservationGroup和CancellationGroup都具有相同的第一个元素 - 一个'reservation-type'元素,在ReservationGroup中分别具有固定值'Reservation',在Cancellationgroup中分别具有'Cancellation'。

Whether this is valid depends on the content of the groups: if they're 'sequence' or 'choice' model groups, it's perfectly legal; 这是否有效取决于团体的内容:如果他们是'序列'或'选择'模型组,它是完全合法的; 'all' model groups are more problematic and generally not allowed in this case. “所有”模型组更有问题,在这种情况下通常不允许。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM