xsd:choice 中元素的 minOccurs 和 maxOccurs [英] minOccurs and maxOccurs on elements inside xsd:choice
问题描述
这是我需要的:
- 子元素可以以任何顺序出现
- 只有 1 个
- 超过 1 个
- 0 或 1
例如:
<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
中的 minOccurs
和 maxOccurs
可能不起作用.当我运行示例时,出现错误:
我该如何解决这个问题?
以下是您的 XSD 内容: 在 a
中,您可以选择以下选项之一:>
- 单个
b
元素. - 一个或多个
c
元素. - 零个或一个
d
元素.
您的 XML 选择选项 #2.它没有第二选择,当解析器遇到 b
元素时,它会正确报告违规.
您可能认为可以通过授予多项选择来解决此问题:
现在会说:在a
中,您可以重复选择以下选项之一:
- 单个
b
元素. - 一个或多个
c
元素. - 零个或一个
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:
- A single
b
element. - One or more
c
elements. - 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:
- A single
b
element. - One or more
c
elements. - 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屋!