XML Schema minOccurs / maxOccurs默认值

我想知道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规范如何处理这些情况,是否存在“官方”定义?

284450 次浏览

minOccursmaxOccurs的默认值为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

一个老问题的新扩展答案

默认值

  • 出现约束minOccursmaxOccurs 默认为1

常见案例说明

<xsd:element name="A"/>

意味着A需要< em > < / em >,必须出现在正好一次< em > < / em >中。


<xsd:element name="A" minOccurs="0"/>

表示A可选< em > < / em >,可能出现最多一次


 <xsd:element name="A" maxOccurs="unbounded"/>

意味着A需要< em > < / em >,并且可以重复无限次


 <xsd:element name="A" minOccurs="0" maxOccurs="unbounded"/>

意味着A可选< em > < / em >,并且可以重复无限次


另请参阅

  • < p > W3C XML模式第0部分:入门

    一般情况下,当值为时,元素必须出现 minOccurs为1或更多。一个元素可以出现的最大次数 的maxOccurs属性的值决定 声明。该值可以是正整数,例如41或 术语无界,表示没有出现的最大次数。 minOccurs和maxOccurs属性的默认值 是1。因此,当像comment这样的元素声明时没有使用 属性时,该元素不能出现多次。一定 如果仅为minOccurs属性指定值,则是 小于或等于maxOccurs的默认值,即它是0或1。 类似地,如果仅为maxOccurs属性指定值,则该值必须大于或等于minOccurs的默认值, 即1个或更多。如果省略了两个属性,则元素必须

    李< /引用> < / >
  • < p > W3C XML模式第1部分:结构第二版

    <element
    maxOccurs = (nonNegativeInteger | unbounded)  : 1
    minOccurs = nonNegativeInteger : 1
    >
    
    
    </element>
    

简短的回答:

用xsd编写:

<xs:attribute name="minOccurs" type="xs:nonNegativeInteger" use="optional" default="1"/>
<xs:attribute name="maxOccurs" type="xs:allNNI" use="optional" default="1"/>

如果您提供一个带有数字的属性,那么这个数字就是边界。否则属性应该只出现一次