XML模式可以在单个complexType中有多个选择



有可能在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")。

有办法吗?

谢谢你的帮助

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

最新更新