定义 XML 模式 (XSD) 时,“选择"“组"元素是否有效 [英] Is it valid to have a 'choice' of 'group' elements when defining an XML Schema (XSD)

查看:49
本文介绍了定义 XML 模式 (XSD) 时,“选择"“组"元素是否有效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在定义 XML 模式 (XSD) 时具有选择"或组"元素是否有效

Is it valid to have a 'choice' or 'group' elements when defining an XML Schema (XSD)

即以下是否有效

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

例如,XML 消息可以表示新预订或取消现有预订.

Where an XML message can represent, for example, either a new reservation or a cancellation of an existing reservation.

如果消息用于预订,则它必须包含在 ReservationGroup 组中定义的所有元素.

If the message is for a reservation, then it must include all the elements defined in the ReservationGroup group.

如果是取消,那么它必须包含在 CancellationGroup 组中定义的所有元素.

If it is a cancellation, then it must include all the elements defined in the CancellationGroup group.

出于某种原因,我的 XML 编辑器 (Eclipse) 不喜欢这样,但没有说明原因.它显示在 <xs:complexType name="HeaderType"> 行上存在错误.但没有说明错误是什么

For some reason, my XML editor (Eclipse) does not like this, but does not indicate why. It shows there being an error on the line <xs:complexType name="HeaderType"> but does not say what the error is

推荐答案

我不是 XML 专家,尽管我经常使用它.这不是我通常做这种结构的方式.我更喜欢单独的复杂类型,而不是选择两个组(请参阅此答案的最后).

I'm no XML expert, although I use it quite a lot. 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).

我怀疑问题在于 ReservationGroup 和 CancellationGroup 以相同的元素开头,在这种情况下,您将违反 Schema Component Constraint: Unique Particle Attribution(如下).

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

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

架构组件约束:唯一粒子属性

内容模型必须形成,以便在·验证·元素信息项目序列,粒子组件直接、间接或包含·隐含地·在其中尝试·验证· 中的每一项序列依次可以是唯一的未经审查就确定该项目的内容或属性,并且没有任何关于剩余部分的项目顺序.

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.

注意:这个约束为 XML Schema 重建[XML 1.0 的等效约束(第二版)] 和 SGML.鉴于元素替换的存在组和通配符,简洁这个约束的表达式是难,见Unique的分析粒子属性约束(非规范性)(§H)进一步讨论.

Note: This constraint reconstructs for XML Schema the equivalent constraints of [XML 1.0 (Second Edition)] and 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.

例如,下面的两个组在同一个选择中是非法的,因为它们的第一个元素都是名称",这意味着您无法确定您正在查看哪个组.然而,ReservationGroup 的第一个元素不同于 Cancellation 组(resDate 和 cancDate 也许),那么那个是有效的.

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. However is the first element of ReservationGroup is different from Cancellation group (resDate and cancDate maybe), then the that is valid.

我以前从未遇到过这种问题,我认为这些组的定义完全合法这一点很有趣,但是如果你把它们放在一起选择,那由于每个组的定义,选择变得非法.

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.

无法形成合法选择的团体

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

可以形成合法选择的团体

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

这篇关于定义 XML 模式 (XSD) 时,“选择"“组"元素是否有效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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