XML模式可以在单个complexType中有多个选项吗?

时间:2011-07-06 21:24:19

标签: java xml xsd schema

是否可以在XML模式中执行类似的操作?

<xsd:complexType name="ItemsType">
  <xsd:choice minOccurs="0" maxOccurs="unbounded">
    <xsd:element ref="shirt"/>
    <xsd:element ref="hat"/>
    <xsd:element ref="umbrella"/>
  </xsd:choice>
  <xsd:choice minOccurs="1" maxOccurs="3">
    <xsd:element ref="apple"/>
    <xsd:element ref="banana"/>
    <xsd:element ref="strawberry"/>
  </xsd:choice>
</xsd:complexType>
但是,这显然是无效的。我想要的是可以有0或更多的第一选择。例如。可能有一个衬衫元素和一个帽子元素,或者根本没有衣服元素(因为minOccurs =“0”),然后是至少1个水果元素(我想要它,所以必须至少有一个,因为的minOccurs = “1”)。

有办法吗?

感谢您的帮助。

1 个答案:

答案 0 :(得分:12)

<xsd:complexType>期望只有一个子元素。将您的两个选项包含在一个<xsd:sequence>中。

实施例

<xsd:complexType name="ItemsType">
  <xsd:sequence>
    <xsd:choice minOccurs="0" maxOccurs="unbounded">
      ... clothes ...
    </xsd:choice>
    <xsd:choice minOccurs="1" maxOccurs="3">
      ... fruits ...
    </xsd:choice>
  </xsd:sequence>
</xsd:complexType>