我想知道XML Schema规范如何处理这些情况:
<xsd:element minOccurs="1" name="asdf"/>
这是基数[1..1]吗?
<xsd:element minOccurs="5" maxOccurs="2" name="asdf"/>
我想这完全无效?
<xsd:element maxOccurs="2" name="asdf"/>
这是基数[0..2]还是[1..2]?
关于XML Schema规范如何处理这些情况,是否存在“官方”定义?
对一个老问题的新的扩展答案……
默认值
发生约束minOccurs和maxOccurs默认为1。
常见案例说明
<xsd:element name="A"/>
意味着A必须出现一次。
<xsd:element name="A" minOccurs="0"/>
意味着A是可选的,最多只能出现一次。
<xsd:element name="A" maxOccurs="unbounded"/>
意味着A是必需的,可以无限次重复。
<xsd:element name="A" minOccurs="0" maxOccurs="unbounded"/>
意味着A是可选的,可以无限次重复。
另请参阅
W3C XML Schema Part 0: Primer
In general, an element is required to appear when the value of
minOccurs is 1 or more. The maximum number of times an element may
appear is determined by the value of a maxOccurs attribute in its
declaration. This value may be a positive integer such as 41, or the
term unbounded to indicate there is no maximum number of occurrences.
The default value for both the minOccurs and the maxOccurs attributes
is 1. Thus, when an element such as comment is declared without a
maxOccurs attribute, the element may not occur more than once. Be sure
that if you specify a value for only the minOccurs attribute, it is
less than or equal to the default value of maxOccurs, i.e. it is 0 or 1.
Similarly, if you specify a value for only the maxOccurs attribute, it must be greater than or equal to the default value of minOccurs,
i.e. 1 or more. If both attributes are omitted, the element must
appear exactly once.
W3C XML Schema Part 1: Structures Second Edition
<element
maxOccurs = (nonNegativeInteger | unbounded) : 1
minOccurs = nonNegativeInteger : 1
>
</element>
minOccurs和maxOccurs的默认值为1。因此:
<xsd:element minOccurs="1" name="asdf"/>
注意:如果只指定minOccurs属性,它不能大于1,因为maxOccurs的默认值是1。
<xsd:element minOccurs="5" maxOccurs="2" name="asdf"/>
无效的
<xsd:element maxOccurs="2" name="asdf"/>
注意:如果只指定maxOccurs属性,它不能小于1,因为minOccurs的默认值是1。
<xsd:element minOccurs="0" maxOccurs="0"/>
使元素变为禁止的有效组合。
欲了解更多信息,请参阅http://www.w3.org/TR/xmlschema-0/#OccurrenceConstraints
对一个老问题的新的扩展答案……
默认值
发生约束minOccurs和maxOccurs默认为1。
常见案例说明
<xsd:element name="A"/>
意味着A必须出现一次。
<xsd:element name="A" minOccurs="0"/>
意味着A是可选的,最多只能出现一次。
<xsd:element name="A" maxOccurs="unbounded"/>
意味着A是必需的,可以无限次重复。
<xsd:element name="A" minOccurs="0" maxOccurs="unbounded"/>
意味着A是可选的,可以无限次重复。
另请参阅
W3C XML Schema Part 0: Primer
In general, an element is required to appear when the value of
minOccurs is 1 or more. The maximum number of times an element may
appear is determined by the value of a maxOccurs attribute in its
declaration. This value may be a positive integer such as 41, or the
term unbounded to indicate there is no maximum number of occurrences.
The default value for both the minOccurs and the maxOccurs attributes
is 1. Thus, when an element such as comment is declared without a
maxOccurs attribute, the element may not occur more than once. Be sure
that if you specify a value for only the minOccurs attribute, it is
less than or equal to the default value of maxOccurs, i.e. it is 0 or 1.
Similarly, if you specify a value for only the maxOccurs attribute, it must be greater than or equal to the default value of minOccurs,
i.e. 1 or more. If both attributes are omitted, the element must
appear exactly once.
W3C XML Schema Part 1: Structures Second Edition
<element
maxOccurs = (nonNegativeInteger | unbounded) : 1
minOccurs = nonNegativeInteger : 1
>
</element>