理解 xsd:choice 和 minOccurs [英] Understanding xsd:choice and minOccurs
问题描述
我无法理解以下 XML 架构的行为:
I am having trouble understanding the behavior of the following XML schema:
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="rootnode">
<xsd:complexType>
<xsd:sequence>
<xsd:choice minOccurs="1" maxOccurs="2">
<xsd:element name="e1" minOccurs="1" maxOccurs="2"/>
<xsd:element name="e2" minOccurs="0" maxOccurs="1"/>
</xsd:choice>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
我希望元素
或
至少有一个实例作为
的子元素代码>.尽管有我的期望,空的
将根据此架构进行验证:
I expected at least one instance of either element <e1>
or <e2>
be required as a child of <rootnode>
. Despite my expectations, an empty <rootnode>
will validate against this schema:
> xmllint --schema test.xsd empty.xml
<?xml version="1.0" encoding="UTF-8"?>
<rootnode>
</rootnode>
empty.xml validates
如果我将元素 e2
的 minOccurs
属性更改为 "0"
以外的其他内容,我会得到我最初预期的行为.
If I change the minOccurs
attribute of element e2
to something other than "0"
, I get the behavior I originally expected.
似乎元素
<e2>
的 absence 被视为xsd:choice
在我的例子.
It seems as though the mere absence of element
<e2>
counts as an occurrence of thexsd:choice
in my example.
如果是这种情况,那么在我的 xsd:choice
中,这种无限出现的次数怎么不违反 maxOccurs
限制?
If this is the case, then how come this infinite number of occurrences does not violate the maxOccurs
limit in my xsd:choice
?
推荐答案
我告诉你,你可以去商店至少一次,最多两次,而且每次你都可以选择买什么:你可以买苹果(一个苹果或两个苹果),或者你可以买橙子(没有橙子或一个橙子).
I tell you you can go to the shops at least once and at most twice, and each time you have a choice of what to buy: you can buy apples (either one apple or two apples), or you can buy oranges (either no oranges or one orange).
您完全有可能选择去商店两次,每次都不买橙子.所以你什么都没有回来.
It's entirely possible that you will choose to go to the shops twice and on each occasion to buy no oranges. So you come back with nothing.
这篇关于理解 xsd:choice 和 minOccurs的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!