XSD描述同一子元素的任意数字-XS:ALL / XS:序列 / XS:选择和红衣主教

发布于 2025-02-04 01:12:49 字数 1123 浏览 3 评论 0原文

我正在尝试描述XSD中的以下XML之类的内容:

<componentDefinitions>
   <component id ="1"/>
   <component id ="2"/>
</componentDefinitions>

还有一些其他约束:

  • 只有一个componentDefinition阻止
  • componentDefiniton block可以是空的,也可以包含一个任意的组件的数量 elements
  • component元素可能会以任何顺序发生

我的解决方案是以下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 of component 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

宫墨修音 2025-02-11 01:12:49

正确的是,有多种指定多个子元素的方法。

就您而言,由于只有同名的元素是可能的孩子,因此无序的的概念是多余的。 (顺便说一句,实际上,无序通常是比任何值得的。) 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 child xs:element with maxOccurs="unbounded".

See also

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文