gpt4 book ai didi

xml - 定义 XML 架构 (XSD) 时,具有 'choice' 元素的 'group' 是否有效

转载 作者:数据小太阳 更新时间:2023-10-29 01:43:13 25 4
gpt4 key购买 nike

定义 XML 架构 (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 消息可以表示新预订或现有预订的取消。

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

如果是取消,则它必须包含 CancellationGroup 组中定义的所有元素。

出于某种原因,我的 XML 编辑器 (Eclipse) 不喜欢这样,但没有说明原因。它显示 行有错误,但没有说明错误是什么

最佳答案

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

我怀疑问题在于 ReservationGroup 和 CancellationGroup 以相同的元素开头,在这种情况下,您将违反架构组件约束:唯一粒子属性(如下)。

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

比如下面这两个组在同一个选择中是非法的,因为它们的第一个元素都是“name”,这意味着你无法识别你在看哪个组。但是 ReservationGroup 的第一个元素是否不同于 Cancellation 组(可能是 resDate 和 cancDate),那么那是有效的。

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

无法形成合法选择的群体

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

正如我上面提到的,我会用复杂类型做这种事情。是的,它增加了另一个元素,但这似乎是显而易见的方式,我喜欢显而易见。

<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 - 定义 XML 架构 (XSD) 时,具有 'choice' 元素的 'group' 是否有效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/101338/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com