XML Schema unique 元素

定义和用法

unique 元素指定属性或元素值(或者属性或元素值的组合)在指定范围内必须是唯一的。 该值必须唯一或为零。

unique 元素必须按顺序包含下列元素:

selector 元素

selector 元素包含 XPath 表达式,指定一个元素集,在其中由 field 元素指定的值必须唯一。

必须有一个且只有一个 selector 元素。

field 元素

每一个 field 元素均包含一个 XPath 表达式,指定对于由 selector 元素指定的元素集而言必须唯一的值(属性或元素值)。

如果有多个 field 元素,则 field 元素的组合必须是唯一的。 在此情况下,单个 field 元素的值对于选定元素不一定是唯一的,但所有字段的组合必须是唯一的。

必须有一个或多个 field 元素。

元素信息

出现次数 一次
父元素 element
内容 annotation、field、selector

语法

<unique
id=ID
name=NCName
any attributes
>

(annotation?,(selector,field+))

</unique>

(? 符号声明在 unique 元素中该元素可出现零次或一次。)

属性 描述
id 可选。规定该元素的唯一的 ID。
name 必需。为该元素指定名称。
any attributes 可选。规定带有 non-schema 命名空间的任何其他属性。

实例

例子 1

本例是一个合并了两个简单类型的简单类型:

<xs:element name="jeans_size">
  <xs:simpleType>
    <xs:union memberTypes="sizebyno sizebystring" />
  </xs:simpleType>
</xs:element>

<xs:simpleType name="sizebyno">
  <xs:restriction base="xs:positiveInteger">
    <xs:maxInclusive value="42"/>
  </xs:restriction>
</xs:simpleType>

<xs:simpleType name="sizebystring">
  <xs:restriction base="xs:string">
    <xs:enumeration value="small"/>
    <xs:enumeration value="medium"/>
    <xs:enumeration value="large"/>
  </xs:restriction>
</xs:simpleType>