What Is "key" Identity-Constraint?

This section describes the 'key' identity-constraint, where a combination of certain sub elements or attributes is defined as a key identity, which must have values and values must be unique.

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

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

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

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

Note that:

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

<?xml version="1.0" encoding="utf-8"?>
<!-- key_identity.xml
 - Copyright (c) 2014, HerongYang.com, All Rights Reserved.
-->
<questions>
 <question>What is Java?</question>
 <question code="John">Who is John?</question>
 <question code="Jim">Where is Jim?</question>
 <question code="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 "code" attributes can be used as a key identity. Here is my XSD schema, key_identity.xsd:

<?xml version="1.0" encoding="utf-8"?>
<!-- key_identity.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:key name="MyKey">
   <xs:selector xpath="./question"/>
   <xs:field xpath="@code"/>
  </xs:key>
 </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="code" type="xs:string"/>
      </xs:extension>
     </xs:simpleContent>
    </xs:complexType>
   </xs:element>
  </xs:sequence>
 </xs:complexType>  
</xs:schema>

The identify-constraint "MyKey" 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 key_identity.xsd -i key_identity.xml

setDocumentLocator(systemId="file:///C:/herong/key_identity.xml", ...
startDocument()
 startElement(name="questions",type="questionsType",attributes={})
  startElement(name="question",type="#AnonType_questionquestionsTy...
[Error] key_identity.xml:6:37: cvc-identity-constraint.4.2.1.a: Element 
^^^ "questions" has no value for the key "MyKey".
  endElement(name="question")
  startElement(name="question",type="#AnonType_questionquestionsTy...
  endElement(name="question")
  startElement(name="question",type="#AnonType_questionquestionsTy...
  endElement(name="question")
[Error] key_identity.xml:9:23: cvc-identity-constraint.4.1: Duplicate 
^^^ unique value [Jim] declared for identity constraint 
^^^ "MyKey" of element "questions".
  startElement(name="question",type="#AnonType_questionquestionsTy...
  endElement(name="question")
 endElement(name="questions")
endDocument()

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

2 validation error messages in the output confirm 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