XSD:有一些选择的序列

问题描述:

我有一个不同类型的序列,对于他们中的一些我想确保,至多使用这些元素中的一个。以下是一些示例:<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'作为其他人的... –

终于让我找到了问题的解决方案:

<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>