XML 架构 - 选择元素内的 maxOccurs [英] XML Schema - maxOccurs within choice element

查看:25
本文介绍了XML 架构 - 选择元素内的 maxOccurs的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下架构声明:

<元素名称="容器"><复杂类型><choice minOccurs="0" maxOccurs="unbounded"><元素名称="动作" minOccurs="0" maxOccurs="1"/><element name="query" minOccurs="0" maxOccurs="unbounded"/><element name="validator" minOccurs="0" maxOccurs="unbounded"/></complexType></元素>

我基本上希望 包含尽可能多的 元素,但是只有一个 元素(可能没有).

据我所知,我不能在 <choice> 上设置 maxOccurs,因为从技术上讲,该选择可以进行无限次(由于查询和验证器无限制).

然而,这个 XML 在 Eclipse 中被认为是有效的(这可能只是 Eclipse 验证中的一个问题,尽管所有其他位都可以正常工作)

<容器><action id="action1" name="action1"/><action id="action2" name="action2"/><query id="query1"/><validator id="testValidator"/></容器>

不确定我是否遗漏了一些明显的东西.

解决方案

您当前的模型定义了以下选择:(a) 一个 action 元素或无,(b) 零个或多个 query 元素,或 (c) 零个或多个 validator 元素,然后允许该选择重复零次或多次.因此它等价于

<元素名称=动作"/><元素名称=查询"/><元素名称=验证器"/>

它允许零个或多个元素的任何序列,每个元素是一个 action、一个 query 或一个 validator 元素.>

使用sequence代替choice可以满足你制定的要求:

<元素名称="动作" minOccurs="0" maxOccurs="1"/><element name="query" minOccurs="0" maxOccurs="unbounded"/><element name="validator" minOccurs="0" maxOccurs="unbounded"/></序列>

有时,不同类型元素出现的顺序会传达信息,因此有必要让它们混合在一起.在这种情况下,问题类似于以下正则表达式问题:编写一个正则表达式,定义由 'a'、'q' 和 'v' 组成的字符串集,其中 'a' 最多出现一次.一个明显的正则表达式是 (q|v)*(a(q|v)?).类似的 XSD 模型组是:

<choice minOccurs="0" maxOccurs="unbounded"><元素引用=查询"/><element ref="验证器"/><sequence minOccurs="0"><元素名称=动作"/><choice minOccurs="0" maxOccurs="unbounded"><元素引用=查询"/><element ref="验证器"/></序列></序列>

(我已从本地元素声明更改为元素引用,以避免必须分别声明 queryvalidator 两次.)

在 XSD 1.1 中,使用 all 组应该可以获得相同的效果:

<元素名称="动作" minOccurs="0" maxOccurs="1"/><element name="query" minOccurs="0" maxOccurs="unbounded"/><element name="validator" minOccurs="0" maxOccurs="unbounded"/></全部>

I've got the following schema declaration:

<element name="container">
  <complexType>
    <choice minOccurs="0" maxOccurs="unbounded">
      <element name="action" minOccurs="0" maxOccurs="1" />
      <element name="query" minOccurs="0" maxOccurs="unbounded" />
      <element name="validator" minOccurs="0" maxOccurs="unbounded" />
    </choice>
  </complexType>
</element>

I basically want a <container> to include as many <query> or <validator> elements as wanted, but only one <action> element (and possibly none).

As far as I know I can't put a maxOccurs on the <choice> since technically that choice can be made an unlimited number of times (due to the unbouded on query and validator).

However, this XML is considered valid in Eclipse (which may just well be a problem in Eclipse validation, although all the other bits work fine)

<container>
  <action id="action1" name="action1" />
  <action id="action2" name="action2" />
  <query id="query1" />
  <validator id="testValidator" />
</container>

Not sure if I'm missing something obvious.

解决方案

Your current model defines a choice among (a) one action element or none, (b) zero or more query elements, or (c) zero or more validator elements, and then allows that choice to be repeated zero or more times. It is thus equivalent to

<choice minOccurs="0" maxOccurs="unbounded">
  <element name="action"/>
  <element name="query"/>
  <element name="validator"/>
</choice>

which allows any sequence of zero or more elements each of which is an action, a query, or a validator element.

The requirements you formulate can be met by using sequence instead of choice:

<sequence>
  <element name="action" minOccurs="0" maxOccurs="1" />
  <element name="query" minOccurs="0" maxOccurs="unbounded" />
  <element name="validator" minOccurs="0" maxOccurs="unbounded" />
</sequence>

Sometimes the sequence in which the different kinds of elements occur conveys information, and it is therefore necessary to allow them to be intermingled. In that case, the problem is analogous to the following reqular-expression problem: write a regular expression defining the set of strings consisting of 'a', 'q', and 'v', in which 'a' occurs at most once. One obvious regex for this is (q|v)*(a(q|v)?). The analogous XSD model group is:

<sequence>
  <choice minOccurs="0" maxOccurs="unbounded">
    <element ref="query"/>
    <element ref="validator"/>
  </choice>
  <sequence minOccurs="0">
    <element name="action"/>
    <choice minOccurs="0" maxOccurs="unbounded">
      <element ref="query"/>
      <element ref="validator"/>
    </choice>
  </sequence>
</sequence>

(I've changed from local element declarations to element references, to avoid having to declare query and validator twice each.)

In XSD 1.1, it should be possible to get the same effect with an all group:

<all>
  <element name="action" minOccurs="0" maxOccurs="1" />
  <element name="query" minOccurs="0" maxOccurs="unbounded" />
  <element name="validator" minOccurs="0" maxOccurs="unbounded" />
</all>

这篇关于XML 架构 - 选择元素内的 maxOccurs的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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