我对XML Schema序列感到困惑根据w3schools.com,
如果必须出现每个元素,它怎么会出现0次?这不会打破必须出现的规则吗?
The sequence element specifies that the child elementsmust appearin a sequence. Each child element can occur from0to any number of times.
另一件事,有什么区别
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="choiceA" type="xs:string" >
<xs:element name="choiceB" type="xs:string" />
</xs:choice>
Run Code Online (Sandbox Code Playgroud)
还有这个:
<xs:sequence minOccurs="0" maxOccurs="unbounded">
<xs:element name="choiceA" type="xs:string" >
<xs:element name="choiceB" type="xs:string" />
</xs:sequence>
Run Code Online (Sandbox Code Playgroud)
你不能为这两种情况都加上任何数量的元素吗?有什么不同吗?
小智 9
序列中的元素必须以架构中指定的顺序出现.但是如果使用minOccurs ="0"定义元素,则不必显示该元素.
这是一个与w3schools.com教程相似的例子.我刚刚将minOccurs ="0"添加到firstname元素中.
<xs:element name="employee">
<xs:complexType>
<xs:sequence>
<xs:element name="firstname" type="xs:string" minOccurs="0"/>
<xs:element name="lastname" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
Run Code Online (Sandbox Code Playgroud)
在这种情况下,基于此定义的有效xml元素将是
<employee>
<firstname>John</firstname>
<lastname>Smith</lastname>
</employee>
Run Code Online (Sandbox Code Playgroud)
或删除firstname元素,因为它有一个minOccurs ="0"
<employee>
<lastname>Smith</lastname>
</employee>
Run Code Online (Sandbox Code Playgroud)
但是你不能混合元素的顺序.所以这将是无效的.
<employee>
<lastname>Smith</lastname>
<firstname>John</firstname>
</employee>
Run Code Online (Sandbox Code Playgroud)
至于选择和顺序之间的区别.choice元素只允许其中一个元素出现.因此,选择可以选择A或选择B,但不能同时选择.尽管定义序列的方式,每个序列元素将同时具有choiceA和choiceB.