XSD - 如何以任意顺序允许任意次数的元素? [英] XSD - how to allow elements in any order any number of times?

查看:43
本文介绍了XSD - 如何以任意顺序允许任意次数的元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个 XSD,并尝试编写具有以下要求的定义:

  • 允许指定的子元素出现任意次数(0 到无界)
  • 允许子元素按任何顺序排列

我环顾四周,找到了各种解决方案,例如 这个:

<xsl:complexType><xs:choice minOccurs="0" maxOccurs="unbounded"><xs:element name="child1" type="xs:int"/><xs:element name="child2" type="xs:string"/></xs:choice></xs:complexType></xs:element>

但据我了解 xs:choice 仍然只允许选择单个元素.因此,像这样将 MaxOccurs 设置为 unbounded 应该只意味着任何一个"子元素可以出现多次.这是准确的吗?

如果上述解决方案不正确,我如何才能实现我在我的要求中所述的内容?

EDIT:如果需求如下怎么办?

  • 元素 child1 child2 可以出现任何次数(0 到无界)
  • 元素的顺序不限
  • 元素 child3 和 child4 应该只出现一次.

例如,此 xml 有效:

<child1>值 </child1><child1>值 </child1><child3>值</child3><child2>值</child2><child4>值 </child4><child1>值 </child1></foo>

但这不是(缺少 child3)

<child1>值 </child1><child1>值 </child1><child2>值</child2><child4>值 </child4><child1>值 </child1></foo>

解决方案

在您问题中的架构中,child1child2 可以以任何顺序出现,任何次数.所以这听起来像是您要找的.

如果您只希望其中一个出现无限次,则 unbounded 将不得不继续使用元素:

<xs:complexType><xs:choice maxOccurs=无界"><xs:元素名称=child1"类型=xs:int";maxOccurs=无界"/><xs:元素名称=child2"类型=xs:字符串";maxOccurs=无界"/></xs:choice></xs:complexType></xs:element>

I am trying to create an XSD, and trying to write the definition with the following requirement:

  • Allow child element specified to appear any number of times (0 to unbounded)
  • Allow child elements to be in any order

I looked around and found various solutions like this:

<xs:element name="foo">
  <xsl:complexType>
    <xs:choice minOccurs="0" maxOccurs="unbounded">
      <xs:element name="child1" type="xs:int"/>
      <xs:element name="child2" type="xs:string"/>
    </xs:choice>
  </xs:complexType>
</xs:element>

But from what I understand xs:choice still only allows single element selection. Hence setting the MaxOccurs to unbounded like this should only mean that "any one" of the child elements can appear multiple times. Is this accurate?

If above solution is incorrect, how can I achieve what I stated above in my requirement?

EDIT: What if the requirement is as follows?

  • Element child1 child2 can appear any number of times (0 to unbounded)
  • Elements to be in any order
  • Elements child3 and child4 should appear exactly once.

For example, this xml is valid:

<foo>
<child1> value </child1>
<child1> value </child1>
<child3> value </child3>
<child2> value </child2>
<child4> value </child4>
<child1> value </child1>
</foo>

but this is not (missing child3)

<foo>
<child1> value </child1>
<child1> value </child1>
<child2> value </child2>
<child4> value </child4>
<child1> value </child1>
</foo>

解决方案

In the schema you have in your question, child1 or child2 can appear in any order, any number of times. So this sounds like what you are looking for.

If you wanted only one of them to appear an unlimited number of times, the unbounded would have to go on the elements instead:

<xs:element name="foo">
   <xs:complexType>
     <xs:choice maxOccurs="unbounded">
       <xs:element name="child1" type="xs:int" maxOccurs="unbounded"/>
       <xs:element name="child2" type="xs:string" maxOccurs="unbounded"/>
     </xs:choice>
   </xs:complexType>
</xs:element>

这篇关于XSD - 如何以任意顺序允许任意次数的元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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