What Is "unique" Identity-Constraint?

This section describes the 'unique' identity-constraint, where a combination of certain sub elements or attributes is defined as an identity, which must have unique values. Missing values are acceptable.

What Is "unique" Identity-Constraint? A "unique" Identity-Constraint is a constraint on an XML element, where a combination of certain sub elements or attributes is defined as an identity, which must have unique values. Missing values are acceptable.

"unique" identity-constraint in XML document is very similar to the concept of UNIQUE constraint on multiple columns in a database table.

Here is the syntax on how a "unique" identity-constraint should be defined in XSD schema:

 <xs:element name="parent" ...>
  ...
  <xs:unique name="constraint-name">
   <xs:selector xpath="sub-element-path"/>
   <xs:field xpath="field-1-path"/>
   <xs:field xpath="field-2-path"/>
   ...
  </xs:unique>
 </xs:element>

Note that:

For example, I have the following XML document, unique_identify.xml:

<?xml version="1.0" encoding="utf-8"?>
<!-- unique_identify.xml
 - Copyright (c) 2014, HerongYang.com, All Rights Reserved.
-->
<questions>
 <question>What is Java?</question>
 <question name="John">Who is John?</question>
 <question name="Jim">Where is Jim?</question>
 <question name="Jim">What is Jim doing?</question>
</questions>

For this XML document, I want to have an XSD schema that has an identity-constraint to ensure values in "name" attributes are unique. Here is my XSD schema, unique_identify.xsd:

<?xml version="1.0" encoding="utf-8"?>
<!-- unique_identify.xsd
 - Copyright (c) 2014, HerongYang.com, All Rights Reserved.
-->
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
 <xs:element name="questions" type="questionsType">
  <xs:unique name="MyUniqueness">
   <xs:selector xpath="./question"/>
   <xs:field xpath="@name"/>
  </xs:unique>
 </xs:element>

 <xs:complexType name="questionsType">
  <xs:sequence>
   <xs:element name="question" maxOccurs="unbounded">
    <xs:complexType>
     <xs:simpleContent>
      <xs:extension base="xs:string">
       <xs:attribute name="name" type="xs:string"/>
      </xs:extension>
     </xs:simpleContent>
    </xs:complexType>
   </xs:element>
  </xs:sequence>
 </xs:complexType>  
</xs:schema>

The identify-constraint "MyUniqueness" is specified inside the "questions" element definition statement. Let's use the "jaxp.TypeInfoWriter" sample program from Xerces2 to validate the XML document to see if the constraint works or not:

>jdk8x2r jaxp.TypeInfoWriter -a unique_identify.xsd 
^^^ -i unique_identify.xml

setDocumentLocator(systemId="file:///C:/herong/unique_identify.xml...
startDocument()
 startElement(name="questions",type="questionsType",attributes={})
  startElement(name="question",type="#AnonType_questionquestionsTy...
  endElement(name="question")
  startElement(name="question",type="#AnonType_questionquestionsTy...
  endElement(name="question")
  startElement(name="question",type="#AnonType_questionquestionsTy...
  endElement(name="question")
[Error] unique_identify.xml:9:23: cvc-identity-constraint.4.1: 
^^^ Duplicate unique value [Jim] declared for identity constraint 
^^^ "MyUniqueness" of element "questions".
  startElement(name="question",type="#AnonType_questionquestionsTy...
  endElement(name="question")
 endElement(name="questions")
endDocument()

I think the "MyUniqueness" constraint worked correctly, and here is what happened:

The validation error message in the output confirms that my above analysis is correct.

Last update: 2014.

Table of Contents

 About This Book

 Introduction to XML Schema

 XML Editor and Schema Processor - XMLPad

 Java API for XML Processing - JAXP

 JAXP - XML Schema (XSD) Validation

 Xerces2 Java Parser - Java API of XML Parsers

 Using Xerces2 Java API

 XML Schema Language - Basics

 Introduction of XSD Built-in Datatypes

 "string" and Its Derived Datatypes

 "decimal" and Its Derived Datatypes

 "dateTime" and Its Related Datatypes

 Miscellaneous Built-in Datatypes

 Facets, Constraining Facets and Restriction Datatypes

 "simpleType" - Defining Your Own Simple Datatypes

 Complex Element Declaration

Identity-Constraints: unique, key and keyref

 What Are Identity-Constraints?

What Is "unique" Identity-Constraint?

 What Is "key" Identity-Constraint?

 What Is "keyref" Identity-Constraint?

 "keyref" Identity-Constraint XSD Example

 Identity-Constraint with Multiple Fields

 Assertion as Custom Validation Rules

 XML Schema Location and Namespace in XML Documents

 Overriding Element Types in XML Documents

 Linking Multiple Schema Documents Together

 Glossary

 References

 PDF Printing Version