在 XML Schema 中,为什么甚至存在元素组标记? [英] In XML Schema, why does an element group tag even exist?

查看:32
本文介绍了在 XML Schema 中,为什么甚至存在元素组标记?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我所知,group 用于创建命名序列(选择、序列、所有),以便以后引用.

From my understanding, group is used to create named sequences (choice, sequence, all) that can later be referenced.

然而,当 complexType 可以用来完成同样的事情并且功能更强大时,为什么这甚至是必要的?这不是很不干燥吗?更不用说group标签本质上既是一个类型的定义,同时也可以是该类型的引用,这取决于你是否使用了refname 属性.这不是非常令人困惑和不必要的吗?还是我遗漏了什么重要的东西?

However, why is this even necessary when complexType can be used to accomplish the same thing and is, in addition, much more powerful? Isn't this very un-DRY? Not to mention that the group tag essentially acts as both a definition of a type, and at the same time, can be a reference to that type, depending on whether you use ref or name attribute. Isn't this terribly confusing and unnecessary? Or am I missing something important?

推荐答案

命名元素和属性组(xs:groupxs:attributeGroup)非常重要.

Named element and attribute groups (xs:group and xs:attributeGroup) are very important.

实际上,它们允许您定义一种代理 complexType,但没有实际 complexType 的限制.

In effect, they allow you to define a sort of surrogate complexTypes, however without the limitations of real complexTypes.

假设您像这样定义基础 complexType:

Suppose you define your base complexType like this:

<xs:complexType name="Base">
  (base element model)
</xs:complexType>

然后,您想在整个 XML 模式中重用 (基本元素模型),您只能通过像这样派生其他复杂类型来做到这一点:

Then, you want to reuse that (base element model) across your XML schema, which you can do only by deriving other complexTypes like this:

<xs:complexType name="Derived">
  <xs:complexContent>
    <xs:extension base="Base">
      (extension element model)
    </xs:extension>
  </xs:complexContent>
</xs:complexType>

但是当扩展 complexType 时,新元素会作为序列添加到基本元素中.新的内容模型如下所示:

But when a complexType is extended, the new elements are added to the base elements as sequence. The new content model will looks as follows:

(base element model), (extension element model)

并且您不能有任何不同(例如,将扩展元素放置在基本元素之前).这就是 XSD 中类型扩展的工作原理!

and you cannot have anything different (e.g. to place extension elements in front of base elements). That's how the type extension in XSD works!

在这里,我可以就此打住,说 XSD 语言就是这样设计的.但是,问题仍然存在.为什么要这样设计?为什么不能更方便地在 XSD 中导出 complexTypes?原因是这样的:派生类型必须符合基类型.你不能有任何东西作为任何东西的延伸作为基础!

And here, I just can stop on this and say that so XSD language was designed. But still, the question remains. Why was it designed so? Why the derivation of complexTypes in XSD could not be made more convenient? The reason is this: The derived type must comply with the base type. You cannot have anything as an extension of anything as a base!

假设您有一个知道如何处理 A 类型元素的软件.软件可能假设实际上 A 类型可能会被扩展,但无论如何,它必须能够在应该是 A 类型的元素中找到它从 A 类型知道/期望的所有内容...并且 A 类型固有的元素内容必须排在第一位!

Suppose you have a software that knows how to process elements of type A. The software may assume that actually the type A might be extended, but in any case, it must be able to find in an element supposed to be of type A everything it knows/expect from type A... and the element content inherent to type A must come the first!

因此,使用 complexType 派生,您无法生成具有如下所示内容模型的 Derived 类型:

So, using complexType derivations you cannot produce a Derived type with a content model looking like this:

(extension element model), (base element model)

也就是说,将扩展元素放在基础元素的前面.

that is, to place the extension elements in front of the base elements.

再一次,你可能想知道为什么?也许,该软件可以做得更复杂,甚至能够解决这个问题?

Again, you may wonder why? Perhaps, the software could be made more sophisticated to be able to sort out even this?

也许吧.但这违背了 XSD 语言中的一项基本原则:任何元素内容模型都必须确定性.这意味着对于任何元素序列,必须始终可以使用单次传递识别特定的内容模型(在 XML 模式中定义).如果软件只知道 A 类型,它必须首先以某种方式传递那个 (扩展元素模型) 的元素,以便找到它知道如何处理的内容.当然,这不可能是确定性的!

Perhaps. But that would be against one of the fundamental principles laid in XSD language: any element content model must be deterministic. That means that for any sequence of elements it must be always possible to recognize a particular content model (defined in an XML schema) using a single pass. If the software knows only about the type A, it must first somehow to pass the elements of that (extension element model) in order to find what it knows how to deal with. That cannot be deterministic, of course!

当你定义一个组时,它不应该表现得像一个独立的 complexType(能够被分配给元素作为它们的类型).它只是一个可以在其他地方重复使用的内容模型.

When you define a group, it is not supposed to behave like a stand-alone complexType (able to be assigned to elements as their type). It is just a piece of content model that can be reused elsewhere.

您想将扩展元素放在基本元素之前吗?没问题!只需将 Base 定义为一个组:

You want to put the extension elements in front of the base elements? No problem! Just define the Base as a group:

<xs:group name="Base">
  (my base element model)
</xs:group>

然后,您可以像这样导出您的 Derived complexType:

Then, you can derive your Derived complexType like this:

<xs:complexType name="Derived">
  <xs:sequence>
    (my extension element model)
    <xs:group ref="Base"/>
  </xs:sequence>
</xs:complexType>

这篇关于在 XML Schema 中,为什么甚至存在元素组标记?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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