简体   繁体   English

xml模式序列相同元素的多个

[英]xml schema sequence multiple of same elements

I am trying to understand example from w3 school about sequence element. 我试图从W3学校了解有关序列元素的示例。 I thought that element inside the sequence can show only once but example from w3 school lead me to believe otherwise. 我认为序列中的元素只能显示一次,但是来自w3学校的示例使我相信不是。 This is example... 这是例子

<xs:element name="pets">
  <xs:complexType>
    <xs:sequence minOccurs="0" maxOccurs="unbounded">
      <xs:element name="dog" type="xs:string"/>
      <xs:element name="cat" type="xs:string"/>
    </xs:sequence>
  </xs:complexType>
</xs:element> 

So explanation on w3 schools says that "This example shows a declaration for an element called "pets" that can have zero or more of the following elements, dog and cat, in the sequence element". 因此,关于w3学校的解释说:“此示例显示了一个名为“ pets”的元素的声明,该元素在sequence元素中可以包含零个或多个以下元素,即dog和cat。 I tried to test that by doing this.... 我试图通过这样做来测试...。

sorry second example i pasted by mistake....this is what i meant that wil not validate based on schema from the top... 抱歉,我错误地粘贴了第二个示例。...这就是我的意思,这是基于顶部的架构无法进行验证的意思...

  <pets>
     <dog>something</dog>
     <dog>something else</dog>
     <cat>else</cat>
  </pets>

But validator gives me errors http://www.corefiling.com/opensource/schemaValidate.html . 但是验证程序给我错误http://www.corefiling.com/opensource/schemaValidate.html I would like to understand both examples. 我想了解两个例子。 Sequence where it is allowable to have multiple of same elements and also where each element in sequence must be present and must show once only. 允许具有多个相同元素的序列,并且每个序列中的元素必须存在并且只能显示一次。

any examples and suggestions would be appreciated. 任何示例和建议,将不胜感激。

That schema allows the content to be zero or more repetitions of the sequence " <dog> followed by <cat> ". 该模式允许内容为序列“ <dog>后跟<cat> ”的零个或更多个重复。 In other words the pets element can be empty, or it can contain dog-cat, dog-cat-dog-cat, etc. but not dog-dog. 换句话说, pets元素可以为空,或者可以包含dog-cat,dog-cat-dog-cat等,但不能包含dog-dog。

If you want any number of dog or cat elements in any combination then you probably want a choice instead of a sequence . 如果要以任意组合使用任意数量的狗或猫元素,则可能需要choice而不是sequence

<xs:element name="pets">
  <xs:complexType>
    <xs:choice minOccurs="0" maxOccurs="unbounded">
      <xs:element name="dog" type="xs:string"/>
      <xs:element name="cat" type="xs:string"/>
    </xs:choice>
  </xs:complexType>
</xs:element> 

This would allow zero or more repetitions of X , where X is either one dog or one cat element. 这将允许X重复零次或更多次,其中X是一只狗或一只猫元素。

The minOccurs / maxOccurs constraints were declared in the sequence element, so they apply to a sequence of (dog, cat) ie one dog , followed by one cat. minOccurs / maxOccurs约束在sequence元素中声明,因此它们适用于(dog, cat)sequence ,即一只 dog ,然后是一只 cat。 You declared that the sequence can repeat, not the individual elements. 您声明了该序列可以重复,而不能重复单个元素。 So your schema will validate documents like this, for example: 因此,您的架构将验证这样的文档,例如:

<pets xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:noNamespaceSchemaLocation="schema6.xsd">
    <dog>something</dog> <!-- sequence 1 -->
    <cat>else</cat>
    <dog>something</dog> <!-- sequence 1 -->
    <cat>else</cat>
    <dog>something</dog> <!-- sequence 1 -->
    <cat>else</cat>
</pets>

The document you posted will not validate because it includes a dog element which does not belong to the sequence allowed: 您发布的文档将无法验证,因为它包含不属于允许序列的dog元素:

   <pets>
     <dog>something</dog> <!-- not allowed - where is the following cat? -->
     <dog>something else</dog> <!-- sequence dog,cat - OK -->
     <cat>else</cat>
   </pets>

Your schema accepts empty sequences (no cat s or dog s) and also unbounded sequences (any amount of cat s and dog s), but there must always be a cat if there is a dog , and a dog if there is a cat , and the dog must come before the cat . 你的模式接受空序列(无cat S或dog S),也无界序列(任何金额cat S和dog S),但必须始终有一个cat如果有dog ,和dog是否有catdog必须在cat前面来。 It's a very strict rule. 这是一个非常严格的规则。

If you want to accept any number of cat s and dog s in any order, you have to redefine those constraints in the individual element declarations, since they are using the defaults and the default is minOccurs="1" maxOccurs="1" : 如果要以任何顺序接受任意数量的catdog ,则必须在各个元素声明中重新定义这些约束,因为它们使用默认值,默认值为minOccurs="1" maxOccurs="1"

<xs:sequence minOccurs="0" maxOccurs="unbounded">
     <xs:element name="dog" type="xs:string" minOccurs="0"/>
     <xs:element name="cat" type="xs:string" minOccurs="0"/>
</xs:sequence>

Now you allow 0 cat s or dog s in each sequence, so you can add dog s and cat s in any order, have only cat s, have only dog s, have empty sequences, have no sequences... 现在您可以在每个序列中允许0个catdog ,因此您可以按任意顺序添加dogcat ,只有cat ,只有dog ,有空序列,没有序列...

<pets>
    <cat>else</cat>
    <cat>else</cat>
    <dog>something</dog>
    <cat>else</cat>
    <dog>something</dog>
    <dog>something</dog>
    <dog>something</dog>
</pets>

(You can also obtain the same result with <xs:choice> as shown in the other answer). (您也可以使用<xs:choice>获得相同的结果,如其他答案所示)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM