2010-07-24 46 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