XSD描述同一子元素的任意数字-XS:ALL / XS:序列 / XS:选择和红衣主教
我正在尝试描述XSD中的以下XML之类的内容:
<componentDefinitions>
<component id ="1"/>
<component id ="2"/>
</componentDefinitions>
还有一些其他约束:
- 只有一个
componentDefinition
阻止 componentDefiniton
block可以是空的,也可以包含一个任意的组件的数量
elementscomponent
元素可能会以任何顺序发生
我的解决方案是以下XSD:
<xs:element name="componentDefinitions">
<xs:complexType>
<xs:choice minOccurs="0">
<xs:element name="component" minOccurs="0" maxOccurs="unbounded">
<xs:complexType>
<xs:attribute name="id" type="xs:string" use="required" />
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
</xs:element>
但是,这是不正确的,同时我声明可以“选择” xs:opeact
的所有内容。
使用XS:所有
似乎是我的正确解决方案,但不允许设置maxoccurs =“无界”
。
XS:sequence
似乎都不正确,因为组件
元素可能按任何顺序发生。
所以这是我的问题:还有其他(更简单)的解决方案吗?
I am trying to describe something like the following XML in XSD:
<componentDefinitions>
<component id ="1"/>
<component id ="2"/>
</componentDefinitions>
There are some additional constraints:
- there is only one
componentDefinition
block - the
componentDefiniton
block can be empty or it contains an arbitrary number ofcomponent
elements component
elements may occur in any order
My solution to this is the following XSD:
<xs:element name="componentDefinitions">
<xs:complexType>
<xs:choice minOccurs="0">
<xs:element name="component" minOccurs="0" maxOccurs="unbounded">
<xs:complexType>
<xs:attribute name="id" type="xs:string" use="required" />
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
</xs:element>
However, this feels not correct since I am using xs:choice
and at the same time I declare that it is ok to "choose" all contents of xs:choice
.
Using xs:all
instead seemed to be the correct solution for me but it is not allowed to set maxOccurs="unbounded"
.
xs:sequence
doesn't seem to be correct either since the component
elements may occur in any order.
So here is my question: Is there any other (more simple) solution to this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正确的是,有多种指定多个子元素的方法。
就您而言,由于只有同名的元素是可能的孩子,因此无序的的概念是多余的。 (顺便说一句,实际上,无序通常是比任何值得的。)
maxoccurs =“无界”
。另请参见
You're right that there are multiple ways of specifying multiple child elements.
In your case, since only elements with the same name are possible children, the notion of unordered is superfluous. (BTW, in practice, unordered is commonly more trouble than it's worth anyway.) What's commonly done in situations like this is to use
xs:sequence
around childxs:element
withmaxOccurs="unbounded"
.See also