2017-07-26 36 views
0

我有一個不同類型的序列,對於他們中的一些我想確保,至多使用這些元素中的一個。以下是一些示例:<Synchronisation><Link>可能會發生一次。有像<TextBox>,<Label>,<CheckBox>等元素。從這些元素,至多有一個允許。 <TextBox>,<Label><CheckBox>XSD:有一些選擇的序列

有效個XML:

<Property> 
    <Synchronisation/> 
</Property> 

<Property> 
    <Synchronisation/> 
    <Link/> 
</Property> 

<Property> 
    <Synchronisation/> 
    <Link/> 
    <TextBox/> 
</Property> 

<Property> 
    <Synchronisation/> 
    <Link/> 
    <Label/> 
</Property> 

無效的XML,爲<TextBox><Label> occures。

<Property> 
    <Synchronisation/> 
    <Link/> 
    <Label/> 
    <TextBox/> 
</Property> 

我試圖做這樣的XSD,但它不工作:

<xsd:complexType name="PropertyType"> 
    <xsd:sequence minOccurs="0"> 
     <xsd:element minOccurs="0" maxOccurs="1" name="Synchronisation" type="SynchronisationType"/> 
     <xsd:element minOccurs="0" maxOccurs="1" name="Links" type="LinksType"/> 
     <xsd:element minOccurs="0" maxOccurs="1" ref="ElementType"/> 
    </xsd:sequence> 
</xsd:complexType> 

<xsd:complexType name="ElementType"> 
    <xsd:choice> 
     <xsd:element name="TextBox" type="TextBoxType"/> 
     <xsd:element name="Label" type="TextBoxType"/> 
     <xsd:element name="CheckBox" type="TextBoxType"/> 
    </xsd:choice> 
</xsd:complexType> 
+0

你是什麼意思「不起作用「? –

+0

在第5行有一個錯誤:''ElementType'元素沒有被聲明爲' – scher

+0

呃,乍一看來,它似乎是使用'ref'而不是'type'作爲其他人的... –

回答

1

終於讓我找到了問題的解決方案:

<xsd:complexType name="PropertyType"> 
    <xsd:sequence minOccurs="0"> 
     <xsd:element minOccurs="0" maxOccurs="1" name="Synchronisation" type="SynchronisationType"/> 
     <xsd:element minOccurs="0" maxOccurs="1" name="Links" type="LinksType"/> 
     <xsd:choice minOccurs="0" maxOccurs="1"/> 
      <xsd:element minOccurs="0" maxOccurs="1" name="TextBox" type="TextBoxType" /> 
      <xsd:element minOccurs="0" maxOccurs="1" name="Label" type="LabelType" /> 
      <xsd:element minOccurs="0" maxOccurs="1" name="CheckBox" type="CheckBoxType" /> 
     </xsd:choice> 
    </xsd:sequence> 
</xsd:complexType>