XSD <xs:all>多次出现无序 [英] XSD &lt;xs:all&gt; with multi occurrences unordered

查看:28
本文介绍了XSD <xs:all>多次出现无序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下 XML

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<props>
<id>someId</id>
<file_size>
    <size>123</size>
    <unit>MB</unit>
</file_size>
<file_size>
    <size>123</size>
    <unit>MB</unit>
</file_size>
</props>

以及相应的 XSD:

<?xml version="1.0" encoding="UTF-8" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
attributeFormDefault="unqualified" elementFormDefault="qualified">
<xs:element name="props">
    <xs:complexType>
        <xs:sequence>
            <xs:element type="xs:string" name="id" />
            <xs:element name="file_size" maxOccurs="unbounded" minOccurs="0">
                <xs:complexType>
                    <xs:sequence>
                        <xs:element type="xs:int" name="size" />
                        <xs:element type="xs:string" name="unit" />
                    </xs:sequence>
                </xs:complexType>
            </xs:element>
        </xs:sequence>
    </xs:complexType>
</xs:element>

我的目标是允许无序元素,我看到了ALL指标解决方案

My goal is to allow unordered elements and i saw ALL indicator solution

但是,我如何维护 file_size 行为?我找到了一些解决方案:

but, how can i maintain the file_size behavior? I found some solutions to put:

<file_size>
    <size>123</size>
    <size>125</size>
    <unit>MB</unit>
    <unit>TB</unit>
</file_size>

但我需要有几个 file_size(一个 size 和一个 unit).

but i need to have several file_size (with one size and one unit).

要恢复,props 的所有子项都可以按任意顺序排列,但具有任意数量的 file_size 元素.

To resume, all of the children of props to be in any order but have any number of file_size elements.

我该如何处理?

推荐答案

这对于 XML Schema 1.0 来说是一个相当尴尬的需求.做任何一个都很容易

This is quite an awkward requirement for XML Schema 1.0. It would be easy to do either of

  • idfile_size 各一个,按任一顺序(使用 all)
  • exactly one each of id and file_size, in either order (using an all)
<xs:all>
    <xs:element type="xs:string" name="id" />
    <xs:element name="file_size">
        <xs:complexType>
            <xs:sequence>
                <xs:element type="xs:int" name="size" />
                <xs:element type="xs:string" name="unit" />
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:all>

  • 任意数量的 id 和任意数量的 file_size 元素,以任意顺序(使用重复的 choice)
    • any number of id and any number of file_size elements, in any order (using a repeating choice)
    • <xs:choice minOccurs="0" maxOccurs="unbounded">
          <xs:element type="xs:string" name="id" />
          <xs:element name="file_size">
              <xs:complexType>
                  <xs:sequence>
                      <xs:element type="xs:int" name="size" />
                      <xs:element type="xs:string" name="unit" />
                  </xs:sequence>
              </xs:complexType>
          </xs:element>
      </xs:choice>
      

      但是要满足您对任意数量的 file_size 元素但恰好一个 id 的特定要求是相当笨拙的.我能看到的最清晰的方法是将 file_size 定义移动到顶层,然后在两个可能的位置出现零次或多次出现 ref :

      but to satisfy your specific requirement of any number of file_size elements but exactly one id is rather clumsy. The clearest way I can see to do it would be to move the file_size definition up to the top level and then ref in zero-or-more occurrences at both possible places:

      <?xml version="1.0" encoding="UTF-8" ?>
      <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
      attributeFormDefault="unqualified" elementFormDefault="qualified">
      
      <xs:element name="file_size">
          <xs:complexType>
              <xs:sequence>
                  <xs:element type="xs:int" name="size" />
                  <xs:element type="xs:string" name="unit" />
              </xs:sequence>
          </xs:complexType>
      </xs:element>
      
      <xs:element name="props">
          <xs:complexType>
              <xs:sequence>
                  <xs:element ref="file_size" minOccurs="0" maxOccurs="unbounded" />
                  <xs:element type="xs:string" name="id" />
                  <xs:element ref="file_size" minOccurs="0" maxOccurs="unbounded" />
              </xs:sequence>
          </xs:complexType>
      </xs:element>
      

      这篇关于XSD <xs:all>多次出现无序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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