XML Schema minOccurs / maxOccurs 默认值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4821477/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
XML Schema minOccurs / maxOccurs default values
提问by Chris
I'm wondering how the XML Schema specification handles these cases:
我想知道 XML Schema 规范如何处理这些情况:
<xsd:element minOccurs="1" name="asdf"/>
No maxOccurs given -> Is this the cardinality [1..1]?
没有给出 maxOccurs -> 这是基数 [1..1] 吗?
<xsd:element minOccurs="5" maxOccurs="2" name="asdf"/>
I suppose this is simply invalid?
我想这简直是无效的?
<xsd:element maxOccurs="2" name="asdf"/>
Is this the cardinality [0..2] or [1..2]?
这是基数 [0..2] 还是 [1..2]?
Is there an "official" definition on how the XML Schema spec handles these cases?
是否有关于 XML Schema 规范如何处理这些情况的“官方”定义?
回答by jasso
The default values for minOccursand maxOccursare 1. Thus:
为默认值minOccurs和maxOccurs是1。因此:
<xsd:element minOccurs="1" name="asdf"/>
cardinality is [1-1] Note: if you specify onlyminOccurs attribute, it can't be greater than 1, because the default value for maxOccurs is 1.
基数为 [1-1] 注意:如果只指定minOccurs 属性,则不能大于 1,因为 maxOccurs 的默认值为 1。
<xsd:element minOccurs="5" maxOccurs="2" name="asdf"/>
invalid
无效的
<xsd:element maxOccurs="2" name="asdf"/>
cardinality is [1-2] Note: if you specify onlymaxOccurs attribute, it can't be smaller than 1, because the default value for minOccurs is 1.
cardinality is [1-2] 注意:如果你只指定了maxOccurs 属性,它不能小于 1,因为 minOccurs 的默认值是 1。
<xsd:element minOccurs="0" maxOccurs="0"/>
is a valid combination which makes the element prohibited.
是使元素被禁止的有效组合。
For more info see http://www.w3.org/TR/xmlschema-0/#OccurrenceConstraints
有关更多信息,请参阅http://www.w3.org/TR/xmlschema-0/#OccurrenceConstraints
回答by kjhughes
New, expanded answer to an old, commonly asked question...
对一个旧的、常见的问题的新的、扩展的答案......
Default Values
默认值
- Occurrence constraints
minOccursandmaxOccursdefault to1.
- 出现次数限制
minOccurs并maxOccurs默认为1。
Common Cases Explained
常见案例说明
<xsd:element name="A"/>
means Ais requiredand must appear exactly once.
meanA是必需的,并且必须恰好出现一次。
<xsd:element name="A" minOccurs="0"/>
means Ais optionaland may appear at most once.
meanA是可选的,最多可能出现一次。
<xsd:element name="A" maxOccurs="unbounded"/>
means Ais requiredand may repeat an unlimited number of times.
手段A是必需的,可以重复无限次。
<xsd:element name="A" minOccurs="0" maxOccurs="unbounded"/>
means Ais optionaland may repeat an unlimited number of times.
手段A是可选的,可以重复无限次。
See Also
也可以看看
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的值为1或更大时,需要一个元素出现。元素可能出现的最大次数由其声明中的 maxOccurs 属性值决定。该值可以是一个正整数,例如 41,或者是无界项,表示没有出现的最大次数。minOccurs 和 maxOccurs 属性的默认值都是 1。因此,当诸如 comment 之类的元素在没有 maxOccurs 属性的情况下声明时,该元素可能不会出现多次。确保如果你只为 minOccurs 属性指定一个值,它小于或等于 maxOccurs 的默认值,即它是 0 或 1。同样,如果你只为 maxOccurs 属性指定一个值,它必须是大于或等于minOccurs 的默认值,即1 或更多。
<element maxOccurs = (nonNegativeInteger | unbounded) : 1 minOccurs = nonNegativeInteger : 1 > </element>
回答by Daniel Perník
Short answer:
简短的回答:
As written in xsd:
如在 xsd 中所写:
<xs:attribute name="minOccurs" type="xs:nonNegativeInteger" use="optional" default="1"/>
<xs:attribute name="maxOccurs" type="xs:allNNI" use="optional" default="1"/>
If you provide an attribute with number, then the number is boundary. Otherwise attribute should appear exactly once.
如果您提供带有数字的属性,则该数字是边界。否则属性应该只出现一次。
回答by Ausra
example:
例子:
XML
XML
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="country.xsl"?>
<country xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="country.xsd">
<countryName>Australia</countryName>
<capital>Canberra</capital>
<nationalLanguage>English</nationalLanguage>
<population>21000000</population>
<currency>Australian Dollar</currency>
<nationalIdentities>
<nationalAnthem>Advance Australia Fair</nationalAnthem>
<nationalDay>Australia Day (26 January)</nationalDay>
<nationalColour>Green and Gold</nationalColour>
<nationalGemstone>Opal</nationalGemstone>
<nationalFlower>Wattle (Acacia pycnantha)</nationalFlower>
</nationalIdentities>
<publicHolidays>
<newYearDay>1 January</newYearDay>
<australiaDay>26 January</australiaDay>
<anzacDay>25 April</anzacDay>
<christmasDay>25 December</christmasDay>
<boxingDay>26 December</boxingDay>
<laborDay>Variable Date</laborDay>
<easter>Variable Date</easter>
<queenBirthDay>21 April (Variable Date)</queenBirthDay>
</publicHolidays>
<states>
<stateName><Name>NSW - New South Wales</Name></stateName>
<stateName><Name>VIC - Victoria</Name></stateName>
<stateName><Name>QLD - Queensland</Name></stateName>
<stateName><Name>SA - South Australia</Name></stateName>
<stateName><Name>WA - Western Australia</Name></stateName>
<stateName><Name>TAS - Tasmania</Name></stateName>
</states>
<territories>
<territoryName>ACT - Australian Capital Territory</territoryName>
<territoryName>NT - Northern Territory</territoryName>
</territories>
</country>
XSD:
XSD:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="country">
<xs:complexType>
<xs:sequence>
<xs:element name="countryName" type="xs:string"/>
<xs:element name="capital" type="xs:string"/>
<xs:element name="nationalLanguage" type="xs:string"/>
<xs:element name="population" type="xs:double"/>
<xs:element name="currency" type="xs:string"/>
<xs:element name="nationalIdentities">
<xs:complexType>
<xs:sequence>
<xs:element name="nationalAnthem" type="xs:string"/>
<xs:element name="nationalDay" type="xs:string"/>
<xs:element name="nationalColour" type="xs:string"/>
<xs:element name="nationalGemstone" type="xs:string"/>
<xs:element name="nationalFlower" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="publicHolidays">
<xs:complexType>
<xs:sequence>
<xs:element name="newYearDay" maxOccurs="1" type="xs:string"/>
<xs:element name="australiaDay" maxOccurs="1" type="xs:string"/>
<xs:element name="anzacDay" maxOccurs="1" type="xs:string"/>
<xs:element name="christmasDay" maxOccurs="1" type="xs:string"/>
<xs:element name="boxingDay" maxOccurs="1" type="xs:string"/>
<xs:element name="laborDay" maxOccurs="1" type="xs:string"/>
<xs:element name="easter" maxOccurs="1" type="xs:string"/>
<xs:element name="queenBirthDay" maxOccurs="1" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="states">
<xs:complexType>
<xs:sequence>
<xs:element name="stateName" minOccurs="1" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="Name" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="territories">
<xs:complexType>
<xs:sequence>
<xs:element name="territoryName" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
XSL:
XSL:
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="html" indent="yes" version="4.0"/>
<xsl:template match="/">
<html>
<body>
<xsl:for-each select="country">
<xsl:value-of select="countryName"/><br/>
<xsl:value-of select="capital"/><br/>
<xsl:value-of select="nationalLanguage"/><br/>
<xsl:value-of select="population"/><br/>
<xsl:value-of select="currency"/><br/>
<xsl:for-each select="nationalIdentities">
<xsl:value-of select="nationalAnthem"/><br/>
<xsl:value-of select="nationalDay"/><br/>
<xsl:value-of select="nationalColour"/><br/>
<xsl:value-of select="nationalGemstone"/><br/>
<xsl:value-of select="nationalFlower"/><br/>
</xsl:for-each>
<xsl:for-each select="publicHolidays">
<xsl:value-of select="newYearDay"/><br/>
<xsl:value-of select="australiaDay"/><br/>
<xsl:value-of select="anzacDay"/><br/>
<xsl:value-of select="christmasDay"/><br/>
<xsl:value-of select="boxingDay"/><br/>
<xsl:value-of select="laborDay"/><br/>
<xsl:value-of select="easter"/><br/>
<xsl:value-of select="queenBirthDay"/><br/>
</xsl:for-each>
<xsl:for-each select="states/stateName">
<xsl:value-of select="Name"/><br/>
</xsl:for-each>
</xsl:for-each>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
Result:
结果:
Australia
Canberra
English
21000000
Australian Dollar
Advance Australia Fair
Australia Day (26 January)
Green and Gold
Opal
Wattle (Acacia pycnantha)
1 January
26 January
25 April
25 December
26 December
Variable Date
Variable Date
21 April (Variable Date)
NSW - New South Wales
VIC - Victoria
QLD - Queensland
SA - South Australia
WA - Western Australia
TAS - Tasmania

