XML 模式,为什么 xs:group 不能是 xs:all 的孩子? [英] XML schema, why xs:group can't be child of xs:all?

查看:30
本文介绍了XML 模式,为什么 xs:group 不能是 xs:all 的孩子?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据此页面 (和我的实践),xs:group 元素不能是 xs:all 的子元素.所以像

According to this page (and my practice), xs:group element cannot be child of xs:all. So something like

<xs:group name="g">
    <xs:element name="first" type="xs:string"/>
    <xs:element name="last" type="xs:string"/>
</xs:group>
<xs:all>
    <xs:group ref="g" minOccurs="0" maxOccurs="1"/>
    <xs:element name="id" type="xs:string"/>
</xs:all>

无效,因为组不能在 xs:all 内.但是我想定义一个模式,其中两个元素(上面示例中的 firstlast )都存在或都不存在,所以我将它们组合成一个组.然后我想让 xs:all 成为组的一部分,因为该组可以以任何顺序与其他元素(例如上面的 id 元素)一起出现.换句话说,我希望有几个元素作为一个整体是可选的.如果 xs:group 不能成为 xs:all 的孩子,我该如何实现?

is not valid because group cannot be inside xs:all. But I want to define a schema, in which two elements (first and last in above example) both exist or neither of them exists, so I make them into a group. Then I want to make the group part of xs:all because the group can appear with other elements (for example, id element above) in any order. In other words, I want to have several elements are optional as a whole group. Without xs:group being able to be child of xs:all, how can I achieve this?

推荐答案

XMLSchema 1.0 只允许在 xs:all 下使用 xs:element(和 xs:annotation).

<all
  id = ID
  maxOccurs = 1 : 1
  minOccurs = (0 | 1) : 1
  {any attributes with non-schema namespace . . .}>
  Content: (annotation?, element*)
</all>

它不允许 xs:groupxs:sequencexs:choice

XML Schema 1.1 许可 xs:element<xs:all:

<all
  id = ID
  maxOccurs = (0 | 1) : 1
  minOccurs = (0 | 1) : 1
  {any attributes with non-schema namespace . . .}>
  Content: (annotation?, (element | any | group)*)
</all>

注意:允许无序元素听起来很理想,但很少真正需要.通常 xs:sequence 在实践中就足够了.

Note: Allowing unordered elements may sound ideal but rarely is truly needed. Usually xs:sequence suffices in practice.

如果您愿意放弃无序要求,您可以(即使在 XSD 1.0 中)要求 firstlast都存在或都不存在"作为如下:

If you are willing to forgo the unordered requirement, you could (even in XSD 1.0) require first and last to "both exist or neither of them exist" as follows:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:xs="http://www.w3.org/2001/XMLSchema">

  <xs:element name="a">
    <xs:complexType>
      <xs:sequence>
        <xs:sequence minOccurs="0">
          <xs:element name="first" type="xs:string"/>
          <xs:element name="last" type="xs:string"/>
        </xs:sequence>
        <xs:element name="id" type="xs:string"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

这篇关于XML 模式,为什么 xs:group 不能是 xs:all 的孩子?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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