xsd:choice 中元素的 minOccurs 和 maxOccurs [英] minOccurs and maxOccurs on elements inside xsd:choice

查看:261
本文介绍了xsd:choice 中元素的 minOccurs 和 maxOccurs的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个根元素 及其子元素

这是我需要的:

例如:

<c/><b/><c/><d/></a>

这是我的 XSD:

<xs:element name="a"><xs:complexType><xs:element name="b"/><xs:element name="c" minOccurs="1" maxOccurs="unbounded"/><xs:element name="d" minOccurs="0" maxOccurs="1"/></xs:choice></xs:complexType></xs:element></xs:schema>

但是 xs:element 中的 minOccursmaxOccurs 可能不起作用.当我运行示例时,出现错误:

<块引用>

元素 不允许出现在元素 下的此位置.

我该如何解决这个问题?

以下是您的 XSD 内容:a 中,您可以选择以下选项之一:

  1. 单个 b 元素.
  2. 一个或多个 c 元素.
  3. 零个或一个 d 元素.

您的 XML 选择选项 #2.它没有第二选择,当解析器遇到 b 元素时,它会正确报告违规.

您可能认为可以通过授予多项选择来解决此问题:

 

现在会说:a中,您可以重复选择以下选项之一:

  1. 单个 b 元素.
  2. 一个或多个 c 元素.
  3. 零个或一个 d 元素.

您的 XML 现在将选择选项 #2,然后是选项 #1,然后是选项 #2,然后是选项 #3,然后声明您的 XML 是有效的.成功了吗?

不,不是,例如,如果您希望确保 只有一个 b 元素子元素,因为选择本身是重复的,并且选项 #1 可以重复选择,每次允许一个 b 元素,但总的来说,有效地允许多个 b 元素.

出于这个原因,xsd:choice 和实际上 XSD 1.0 通常不能用来表示您的约束.

您的选择?在 XSD 之外的代码中检查此约束,或使用 XSD 1.1 的 xsd:assert:

XSD 1.1 解决方案

<xs:element name="a"><xs:complexType><xs:choice maxOccurs="unbounded"><xs:element name="b"/><xs:element name="c"/><xs:element name="d"/></xs:choice><xs:assert test="count(b) = 1 and count(c) > 1 and count(d) >= 0"/></xs:complexType></xs:element></xs:schema>

I have a root element <a> and its children <b>, <c>, <d>

Here's what I need:

  • the child elements can appear in any order
  • just 1 <b>
  • more than 1 <c>
  • 0 or 1 <d>

For example:

<a>
    <c />
    <b />
    <c />
    <d /> 
</a>

And here is my XSD:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
           elementFormDefault="qualified"
           attributeFormDefault="unqualified">
  <xs:element name="a">
    <xs:complexType>
      <xs:choice>
        <xs:element name="b" />
        <xs:element name="c" minOccurs="1" maxOccurs="unbounded"/>
        <xs:element name="d" minOccurs="0" maxOccurs="1"/>
      </xs:choice>
    </xs:complexType>
  </xs:element>
</xs:schema>

But the minOccurs and maxOccurs in xs:element may not work. When I run the example, I got an error:

Element <b> is not allowed at this location under element <a>.

How can I fix this?

解决方案

Here's what your XSD says: Within a you can choose one of the following options:

  1. A single b element.
  2. One or more c elements.
  3. Zero or one d elements.

Your XML chooses option #2. It doesn't get a second choice, and when the parser encounters the b element, it correctly reports the violation.

You might think you could fix this by granting multiple choices:

  <xs:choice maxOccurs="unbounded">

This would now say: Within a you can repeatedly choose one of the following options:

  1. A single b element.
  2. One or more c elements.
  3. Zero or one d elements.

Your XML would now choose option #2, then option #1, then option #2, then option #3, then declare that your XML is valid. Success?

No, not if, for example, you wish to ensure only one b element child because the choice itself is repeated, and option #1 can be repeatedly selected, each time allowing a single b element, but in aggregate, effectively allowing multiple b elements.

For this reason, xsd:choice and, in fact, XSD 1.0 in general cannot be used to represent your constraint.

Your options? Check this constraint in code outside of XSD, or use XSD 1.1's xsd:assert:

XSD 1.1 Solution

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
           xmlns:vc="http://www.w3.org/2007/XMLSchema-versioning"
           vc:minVersion="1.1">
  <xs:element name="a">
    <xs:complexType>
      <xs:choice maxOccurs="unbounded">
        <xs:element name="b" />
        <xs:element name="c"/>
        <xs:element name="d"/>
      </xs:choice>
      <xs:assert test="count(b) = 1 and count(c) > 1 and count(d) >= 0"/>
    </xs:complexType>
  </xs:element>
</xs:schema>

这篇关于xsd:choice 中元素的 minOccurs 和 maxOccurs的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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