Compartilhar via


<xsd:unique> Element

Specifies that an attribute or element value (or a combination of attribute or element values) must be unique within the specified scope. The value must be unique or nil.

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

Attributes

  • id
    The ID of this element. The id value must be of type ID and be unique within the document containing this element.

    Optional.

  • name
    The name of the unique element. The name must be a no-colon-name (NCName) as defined in the XML Namespaces specification.

    The name must be unique within an identity constraint set.

    Required.

Element Information

Number of occurrences

One time

Parent elements

element

Contents

annotation, field, selector

Remarks

The unique element must contain the following elements in order.

selector

The selector element contains an XML Path Language (XPath) expression specifying the set of elements across which the values specified by the field elements must be unique.

There must be one and only one selector element.

field

Each field element contains an XPath expression specifying the values (attribute or element values) that must be unique for the set of elements specified by the selector element.

If there is more than one field element, the combination of the field elements must be unique. In this case, the values for a single field element may or may not be unique across the selected elements, but the combination of all the fields must be unique.

There must be one or more field element.

Example

The following example defines a schema with a root element of ordersByCustomer, which can contain an unbounded sequence of customerOrders elements. In the ordersByCustomer element, the unique element specifies that the customerID attribute must be unique across all 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>

Other Resources

For more information see the W3C XML Schema Part 1: Structures Recommendation at www.w3.org/TR/2001/REC-xmlschema-1-20010502/\#element-all.

See Also

Reference

XML Schemas (XSD) Reference
XML Schema Elements