2010-07-24 110 views
30

我有以下XSD代码:使用XSD进行XML验证:如何避免关注元素的顺序?

<xsd:complexType name="questions"> 
    <xsd:sequence> 
     <xsd:element name="location" type="location"/> 
     <xsd:element name="multipleChoiceInput" type="multipleChoiceInput" minOccurs="0" maxOccurs="unbounded"/> 
     <xsd:element name="textInput" type="textInput" minOccurs="0" maxOccurs="unbounded"/> 
     <xsd:element name="pictureInput" type="pictureInput" minOccurs="0"/> 
    </xsd:sequence> 
</xsd:complexType> 

这里的问题是:该元素的位置,multipleChoiceInput等必须出现在它们被声明的顺序相同。我不希望发生这种情况,我想要的是,在验证过程中,序列不应该是相关的。我怎样才能做到这一点?

我试过另一种可能性已经:

<xsd:complexType name="questions"> 

     <xsd:choice maxOccurs="unbounded"> 
      <xsd:element name="location" type="location"/> 
      <xsd:element name="multipleChoiceInput" type="multipleChoiceInput" minOccurs="0" maxOccurs="unbounded"/> 
      <xsd:element name="textInput" type="textInput" minOccurs="0" maxOccurs="unbounded"/> 
      <xsd:element name="pictureInput" type="pictureInput" minOccurs="0" maxOccurs="1"/> 
     </xsd:choice>    

</xsd:complexType> 

在这个例子中,该序列真的没关系了,我能有这么多的元素,因为我想(什么是“所有”不会允许我去做)。但是我仍然遇到min-和maxOccurs的问题。在这个例子中,我可以有这么多的“pictureInput”,我想要的是0或1的限制是什么。

非常感谢您的帮助!

回答

39
<xsd:complexType name="questions"> 
    <xsd:all> 
     <xsd:element name="location" type="location"/> 
     <xsd:element name="multipleChoiceInput" type="multipleChoiceInput"/> 
     <xsd:element name="textInput" type="textInput"/> 
     <xsd:element name="pictureInput" type="pictureInput"/> 
    </xsd:all> 
</xsd:complexType> 

注:我已经更改为 “所有”

序列势力顺序(定义) “序列”。如果订单并不重要,那么全部都被使用。

如果有多次出现元素的机会,则可以使用xsd:any。

<xsd:complexType name="questions"> 
    <xsd:sequence> 
     <xsd:any minOccurs="0"/> 
    </xsd:sequence> 
</xsd:complexType> 

你可以找到XSD的细节:在任何以下链接:

http://www.w3schools.com/schema/schema_complex_any.asp

+2

谢谢,但“所有”不能在我的情况,原因使用“全部”要求元素仅出现一次(min-和maxOccurs只能接受值0和1)。 – jcborges 2010-07-24 14:18:14

+1

然后,也许''是你的朋友。 – Tomalak 2010-07-24 14:19:39

+1

雅在这种情况下任何需要使用。也会更新答案。 – YoK 2010-07-24 14:42:12

16

我有点晚了这次讨论,但我有同样的问题,并找到了解决办法:

<xsd:complexType name="questions"> 
    <xsd:choice maxOccurs="unbounded"> 
     <xsd:element name="location" type="location"/> 
     <xsd:element name="multipleChoiceInput" type="multipleChoiceInput"/> 
     <xsd:element name="textInput" type="textInput"/> 
     <xsd:element name="pictureInput" type="pictureInput"/> 
    </xsd:choice> 
</xsd:complexType> 

关键是将xs:choice与maxOccurs =“unbounded”结合起来。如果你只是使用xs:all,那么每个周期都允许有一个。

编辑为增加: 虽然xs:any会有效,但它不会限制您对四项元素的选择。它将允许任何事情,这几乎无视了模式的目的。

+1

对我来说,这是解决这个问题的最好方法,尽管它并不完美。在这种情况下,这不符合具有0或1个“pictureInput”的要求。您可以添加多个1,并且设置maxOccurs不能防止这种情况(因为选择本身是未绑定的)。 – 2015-02-12 15:30:36

0

也很晚了这里的聚会,但会结合使用<xsd:all>minOccursmaxOccurs不回答郁慕明工作?:

<xsd:complexType name="questions"> 
    <xsd:all> 
     <xsd:element name="location" type="location" minOccurs="0" maxOccurs="1"/> 
     <xsd:element name="multipleChoiceInput" type="multipleChoiceInput" minOccurs="0" maxOccurs="1"/> 
     <xsd:element name="textInput" type="textInput" minOccurs="0" maxOccurs="1"/> 
     <xsd:element name="pictureInput" type="pictureInput" minOccurs="0" maxOccurs="1"/> 
    </xsd:all> 
</xsd:complexType> 
+1

不,因为在任何内部你都不能定义maxOccurs大于1 – sotix 2015-06-11 06:47:43