子元素中没有重复元素?

时间:2016-03-03 18:25:34

标签: xml xsd

想象一下,我有以下复杂类型:

<xs:complexType name="pet">
    <xs:sequence>
        <xs:element name="add" type="xs:string" minOccurs="1" maxOccurs="unbounded" />
    </xs:sequence>
</xs:complexType>

<xs:complexType name="pets">
    <xs:sequence>
        <xs:element name="dogs" type="pet" minOccurs="0" maxOccurs="1" />
        <xs:element name="cats" type="pet" minOccurs="0" maxOccurs="1" />
        <xs:element name="hamsters" type="pet" minOccurs="0" maxOccurs="1" />
        <xs:element name="horses" type="pet" minOccurs="0" maxOccurs="1" />
    </xs:sequence>
</xs:complexType>

这意味着我可以像这样指定我的xml:

<pets>
    <dogs>
        <add>Fido</add>
        <add>Pluto</add>
    </dogs>
    <cats>
        <add>Zorro</add>
        <add>Batman</add>
    </cats>
    <hamsters>
        <add>Bob</add>
    </hamsters>
    <horses>
        <add>Mr Horse</add>
    </horses>
</pets>

但我真正想做的是让我的架构要求至少定义了一个pet类型。如果我将pets类型更改为:

<xs:complexType name="pets">
    <xs:choice  minOccurs="1" maxOccurs="unbounded">
        <xs:element name="dogs" type="pet" />
        <xs:element name="cats" type="pet" />
        <xs:element name="hamsters" type="pet" />
        <xs:element name="horses" type="pet" />
    </xs:choice>
</xs:complexType>

这种方式使其有效,因为现在要求我至少定义了一个pet。但是,现在有多个pet个相同类型的元素是有效的。这是我想要避免的。以下内容现在有效:

<pets>
    <dogs>
        <add>Pluto</add>
    </dogs>
    <dogs>
        <add>Fido</add>
    </dogs>
</pets>

那么,每种类型只能接受一个pet元素吗?

1 个答案:

答案 0 :(得分:0)

XSD 1.0

@ 5gon12eder的行suggested

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="pets">
    <xs:complexType>
      <xs:choice>
        <xs:sequence>
          <xs:element name="dogs" type="pet"/>
          <xs:element name="cats" type="pet" minOccurs="0"/>
          <xs:element name="hamsters" type="pet" minOccurs="0"/>
          <xs:element name="horses" type="pet" minOccurs="0"/>
        </xs:sequence>
        <xs:sequence>
          <xs:element name="cats" type="pet"/>
          <xs:element name="dogs" type="pet" minOccurs="0"/>
          <xs:element name="hamsters" type="pet" minOccurs="0"/>
          <xs:element name="horses" type="pet" minOccurs="0"/>
        </xs:sequence>
        <xs:sequence>
          <xs:element name="hamsters" type="pet"/>
          <xs:element name="dogs" type="pet" minOccurs="0"/>
          <xs:element name="cats" type="pet" minOccurs="0"/>
          <xs:element name="horses" type="pet" minOccurs="0"/>
        </xs:sequence>
        <xs:sequence>
          <xs:element name="horses" type="pet"/>
          <xs:element name="dogs" type="pet" minOccurs="0"/>
          <xs:element name="cats" type="pet" minOccurs="0"/>
          <xs:element name="hamsters" type="pet" minOccurs="0"/>
        </xs:sequence>
      </xs:choice>
    </xs:complexType>
  </xs:element>
  <xs:complexType name="pet">
    <xs:sequence>
      <xs:element name="add" type="xs:string" minOccurs="1" maxOccurs="unbounded" />
    </xs:sequence>
  </xs:complexType>
</xs:schema>

XSD 1.1

使用xs:assert确保至少有一个pets的孩子:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
           xmlns:vc="http://www.w3.org/2007/XMLSchema-versioning" vc:minVersion="1.1">

  <xs:element name="pets">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="dogs" type="pet" minOccurs="0"/>
        <xs:element name="cats" type="pet" minOccurs="0"/>
        <xs:element name="hamsters" type="pet" minOccurs="0"/>
        <xs:element name="horses" type="pet" minOccurs="0"/>
      </xs:sequence>
      <xs:assert test="*"/>
    </xs:complexType>
  </xs:element>

  <xs:complexType name="pet">
    <xs:sequence>
      <xs:element name="add" type="xs:string" minOccurs="1" maxOccurs="unbounded" />
    </xs:sequence>
  </xs:complexType>

</xs:schema>