<xsd:unique> 元素

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

<unique
  id = ID 
  name = NCName 
  {any attributes with non-schema Namespace}...>
Content: (annotation?, (selector, field+))
</unique>

特性

  • id
    该元素的 ID。 id 值必须属于类型 ID 并且在包含该元素的文档中是唯一的。

    可选。

  • name
    unique 元素的名称。 该名称必须是在 XML 命名空间规范中定义的无冒号名称 (NCName)。

    该名称在标识约束集中必须是唯一的。

    必需。

元素信息

出现次数

一次

父元素

元素

内容

annotationfieldselector

备注

unique 元素必须按顺序包含以下元素。

selector

selector 元素包含 XML 路径语言 (XPath) 表达式,指定一个元素集,在其中由 field 元素指定的值必须唯一。

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

Field — 字段

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

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

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

示例

以下示例通过 ordersByCustomer 的根元素定义一个架构,该架构可以包含无限制的 customerOrders 元素的序列。 在 ordersByCustomer 元素中,unique 元素指定 customerID 属性在所有 customerOrders 中必须是唯一的。

<xs:schema targetNamespace="http://tempuri.org/myschema/unique" elementFormDefault="qualified"
 xmlns="http://tempuri.org/myschema/unique"
 xmlns:mstns="http://tempuri.org/myschema/unique"
 xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:complexType name="customerOrderType">
 <xs:sequence>
  <xs:element name="item" minOccurs="0" maxOccurs="unbounded">
    <xs:complexType>
    <xs:attribute name="itemID" type="xs:string"/>
    </xs:complexType>
  </xs:element>
 </xs:sequence>
 <xs:attribute name="CustomerID" type="xs:string"/>
</xs:complexType>

<xs:element name="ordersByCustomer">
 <xs:complexType>
    <xs:sequence>
     <xs:element name="customerOrders" type="customerOrderType" 
       minOccurs="0" maxOccurs="unbounded"/>
    </xs:sequence>
 </xs:complexType>
 <xs:unique name="oneCustomerOrdersforEachCustomerID">
  <xs:selector xpath="mstns:customerOrders"/>
  <xs:field xpath="@CustomerID"/>
 </xs:unique>
</xs:element>
</xs:schema>

其他资源

有关更多信息,请参见“W3C XML 架构第 1 部分:结构建议”中的相关部分,网址为:www.w3.org/TR/2001/REC-xmlschema-1-20010502/#element-all。

请参见

参考

XML 架构 (XSD) 参考

XML 架构元素