了解 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>
我希望元素 <e1>
或 <e2>
的至少一个实例需要作为 <rootnode>
的子元素代码>.尽管我的期望是,一个空的 <rootnode>
将针对此架构进行验证:
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屋!