随机顺序的元素和 maxOccurs >1 [英] Elements in random order and maxOccurs > 1

查看:28
本文介绍了随机顺序的元素和 maxOccurs >1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要创建一个 XSD 1.0 来验证 XML 文件.

I need to create a XSD 1.0 that validates a XML file.

验证将使用 python 中的 lxml.etree,并且此工具仅支持 XML Schema 1.0 (lxml with schema 1.1)

The validation will use lxml.etree from python, and this tool is supporting just XML Schema 1.0 (lxml with schema 1.1)

我需要使用的结构是这样的:

The structure that I need to use is of type:

    item
    | owners*
    | config+
    | | config_id
    | | tests*
    | | picked?
    | | capability*
    | | | name
    | | | value

使用的符号是:

  • * 元素可以出现零次或多次.
  • + 元素可以出现一次或多次.
  • ? 该元素是可选的.
  • * The element can occur zero or more times.
  • + The element can occur one or more times.
  • ? The element is optional.

config 标签中的元素可以是任何顺序,这意味着我不能使用 指示符.<all> 指标给我随机顺序,但在这种情况下 maxOccurs 是 1. 指标与 maxOccurs="unbounded" 会给我随机顺序和元素的多个数量,但元素没有下限.

Elements in config tag can be in any order, this means that I can't use <sequence> indicator. <all> indicator is giving me the random order, but in this case the maxOccurs is 1. <choice> indicator with maxOccurs="unbounded" will give me the random order and the multiple number of elements, but there will be no bottom limit for elements.

我的 XSD 文件类似于:

My XSD file looks something like:

<?xml version="1.0" encoding="ISO-8859-1" ?>

<!--Schema version: 1.0, date: 29-02-2016-->
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">

  <!-- definition of complex types -->    
  <xs:complexType name="capability_type">
    <xs:sequence>
      <xs:element name="name" type="xs:string" />
      <xs:element name="value" type="xs:string" />
    </xs:sequence>
  </xs:complexType>

  <xs:complexType name="config_type">
    <xs:all>
      <xs:element name="config_id" type="xs:string" />
      <xs:element name="tests" type="xs:string" minOccurs="0" 
                          maxOccurs="unbounded" />
      <xs:element name="picked" type="xs:string" minOccurs="0" />
      <xs:element name="capability" type="capability_type" 
                          minOccurs="0" maxOccurs="unbounded" />
    </xs:all>
  </xs:complexType>

  <xs:complexType name="item_type">
    <xs:sequence>
      <xs:element name="owners" type="xs:string" minOccurs="0" 
                          maxOccurs="unbounded" />
      <xs:element name="config" type="config_type" minOccurs="1" 
                          maxOccurs="unbounded" />
    </xs:sequence>
  </xs:complexType>

  <!-- definition of schema -->
  <xs:element name="item" type="item_type" />

</xs:schema>

使用这个模式我收到错误:

Using this schema I receive error:

元素元素:架构解析器错误:元素'{http://www.w3.org/2001/XMLSchema}元素':无效价值maxOccurs(必须是 0 或 1).

element element: Schemas parser error : Element '{http://www.w3.org/2001/XMLSchema}element': Invalid value for maxOccurs (must be 0 or 1).

我的问题有什么替代方案吗?

Is there any alternatives for my problem ?

推荐答案

是的,有替代方案:

  1. 完全放弃随机顺序要求.它往往比它的价值更麻烦.
  2. 通过将 config_idpicked 迁移到 xs:all 之外并迁移 maxOccurs="unbounded 来部分放弃随机顺序要求"xs:all 的孩子到 xs:all 本身.
  3. 保持随机顺序要求,将 maxOccurs="unbounded" 迁移到 xs:all,并使用 XSD 1.1 断言强制执行其他出现约束.
  1. Completely abandon the random order requirement. It tends to be more trouble than it's worth.
  2. Partially abandon the random order requirement by migrating config_id and picked outside of the xs:all and migrating maxOccurs="unbounded" from the children of xs:all to xs:all itself.
  3. Keep the random order requirement, migrate maxOccurs="unbounded" to xs:all, and use XSD 1.1 assertions to enforce the other occurrence constraints.

这篇关于随机顺序的元素和 maxOccurs >1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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