<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.

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


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


  • 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.


Element Information

Number of occurrences

One time

Parent elements



annotation, field, selector


The unique element must contain the following elements in order.


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.


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.


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"
<xs:complexType name="customerOrderType">
  <xs:element name="item" minOccurs="0" maxOccurs="unbounded">
    <xs:attribute name="itemID" type="xs:string"/>
 <xs:attribute name="CustomerID" type="xs:string"/>

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

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-al.

See Also


XML Schemas (XSD) Reference
XML Schema Elements