一种模式,允许元素以任何顺序但没有重复?

时间:2012-12-01 21:47:15

标签: xml xsd

我正在尝试编写一个XSD架构,其中某些元素可以按任何顺序出现 - 但不能重复。

所以这是有效的:

<parent>
    <my-element-A>1</my-element-A>
    <my-element-B>2</my-element-B>
    <my-element-C>3</my-element-C>
</parent>

......这是有效的:

<parent>
    <my-element-B>2</my-element-B>
    <my-element-A>1</my-element-A>
    <my-element-C>3</my-element-C>
</parent>

但这不是:

<parent>
    <my-element-A>1</my-element-A>
    <my-element-B>2</my-element-B>
    <my-element-A>1</my-element-A><!-- Fail! Duplicate my-element-A -->
    <my-element-C>3</my-element-C>
</parent>

如何使用XSD执行此操作?

1 个答案:

答案 0 :(得分:2)

<xs:all>元素定义"A grouping of a sequence is a set of sub-sequences, some or all of which may be empty, such that each member of the original sequence appears once and only once in one of the sub-sequences and all members of all sub-sequences are in the original sequence."

  <?xml version="1.0" encoding="UTF-8"?>
  <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
    <xs:element name="parent">
      <xs:complexType>
        <xs:all minOccurs="0">
          <xs:element name="my-element-A" type="xs:string"/>
          <xs:element name="my-element-B" type="xs:string"/>
          <xs:element name="my-element-C" type="xs:string"/>
        </xs:all>
      </xs:complexType>
    </xs:element>
  </xs:schema>