Specific order of two elements inside xsd:choice

时间:2016-03-02 11:02:12

标签: xml xsd

I have the following XSD:

<xs:complexType name="CodeTags">
    <xs:choice maxOccurs="unbounded">
        <xs:element name="text" minOccurs="0" maxOccurs="unbounded">
            ...
        </xs:element>
        <xs:element name="reference" minOccurs="0" maxOccurs="unbounded">
            ...
        </xs:element>
        <xs:element ref="switch" minOccurs="0" maxOccurs="unbounded">
            ...
        </xs:element>
        <xs:element ref="if" minOccurs="0" maxOccurs="unbounded">
            ...
        </xs:element>
        <xs:element ref="else" minOccurs="0" maxOccurs="unbounded">
            ...
        </xs:element>
        <xs:element ref="param" minOccurs="0" maxOccurs="unbounded">
            ...
        </xs:element>       
    </xs:choice>
</xs:complexType>

I want that the else tag just appears after the if tag. How can I achieve this? I already put them into a <xs:sequence> order but I was still able to enter an else tag anywhere with other tags between it and the if tag.

EDIT: Code snippet was posted as a picture, now changed to a code block.

2 个答案:

答案 0 :(得分:2)

只需将ifelse放在xs:sequence作为选项之一:

<xs:choice minOccurs="0" maxOccurs="unbounded">
  <!-- ... -->
  <xs:sequence>
    <xs:element ref="if"/>
    <xs:element ref="else"/>
  </xs:sequence>
  <!-- ... -->
<xs:choice>

您还可以从每个选项中删除minOccursmaxOccurs;将它们放在xs:choice本身就足够了。

答案 1 :(得分:1)

为了更好地学习如何处理这种情况,我建议你首先要了解你编写的CodeTags元素的含义,以及为什么它允许你不想允许的元素序列。然后继续理解为什么kjhughes提供的解决方案有效。