XSD:有一些选择的顺序



我有一个不同类型的序列,对于其中一些,我想确保最多使用其中一个元素。以下是一些示例:<Synchronisation><Link>可能会发生一次。有元素如<TextBox><Label><CheckBox>等。从这些元素中最多允许一个。要么<TextBox>,要么<Label>,要么<CheckBox>

有效的 XML:

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

无效的 XML,如出现<TextBox><Label>

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

最后我找到了解决问题的方法:

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

最新更新