问题描述
我正在尝试创建 XSD,并尝试编写具有以下要求的定义:
I am trying to create an XSD, and trying to write the definition with the following requirement:
- 允许指定的子元素出现任意次数(0 到无界)
- 允许子元素按任意顺序排列
我环顾四周,找到了各种解决方案,例如 这个:
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>
但据我了解 xs:choice 仍然只允许选择单个元素.因此,像这样将 MaxOccurs 设置为无界应该只意味着任何一个"子元素可以出现多次.这是准确的吗?
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?
- 元素 child1 child2 可以出现任何次数(0 到无限制)
- 元素可以任意排列
- 元素 child3 和 child4 应该只出现一次.
例如,此 xml 有效:
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>
但这不是(缺少孩子3)
but this is not (missing child3)
<foo>
<child1> value </child1>
<child1> value </child1>
<child2> value </child2>
<child4> value </child4>
<child1> value </child1>
</foo>
推荐答案
在您的问题的架构中,child1
或 child2
可以按任何顺序出现,任何次数.所以这听起来像您正在寻找的东西.
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 - 如何允许任意顺序的元素任意次数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!