Identity-Constraint with Multiple Fields

This section provides a tutorial example on how to specify a 'key' identify constraint in an XSD schema that uses multiple 'field' to construct identify values with multiple components.

Identify-constraints, "unique", "key", and "keyref" can support identities that use multiple fields. Here is a sample XML document, key_identity_3_fields.xml:

<?xml version="1.0" encoding="utf-8"?>
<!-- key_identity_3_fields.xml
 - Copyright (c) 2014,, All Rights Reserved.
 <person fname="Bill" lname="Gates" dob="1955-10-28"/>
 <person fname="Joe" lname="Gates" dob="1955-10-28"/>
 <person fname="Joe" lname="Bush" dob="1955-04-19"/>
 <person fname="Bill" lname="Gates" dob="1955-10-28"/>
 <person fname="Bill" lname="Bush"/>

For this XML document, I want to have an XSD schema that has an identity-constraint to ensure the combination "fname", "lname" and "dob" attribute values become a key identity. Here is my XSD schema, key_identity_3_fields.xsd:

<?xml version="1.0" encoding="utf-8"?>
<!-- key_identity_3_fields.xsd
 - Copyright (c) 2014,, All Rights Reserved.
<xs:schema xmlns:xs="">
 <xs:element name="persons" type="personsType">
  <xs:key name="MyKey">
   <xs:selector xpath="./person"/>
   <xs:field xpath="@fname"/>
   <xs:field xpath="@lname"/>
   <xs:field xpath="@dob"/>

 <xs:complexType name="personsType">
   <xs:element name="person" maxOccurs="unbounded">
      <xs:extension base="xs:string">
       <xs:attribute name="fname" type="xs:string"/>
       <xs:attribute name="lname" type="xs:string"/>
       <xs:attribute name="dob" type="xs:string"/>

The identify-constraint "MyKey" is specified inside the "persons" element definition statement. Notice that 3 "field" statements are used to build the identity with multiple data components. Let's use the "jaxp.TypeInfoWriter" sample program from Xerces2 to validate the XML document to see if this constraint works or not:

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

[Error] key_identity_3_fields.xml:9:55: cvc-identity-constraint.4.2.2:
^^^ Duplicate key value [Bill,Gates,1955-10-28] declared for identity 
^^^ constraint "MyKey" of element "persons".
[Error] key_identity_3_fields.xml:10:37: cvc-identity-constraint
^^^ .4.2.1.b: Not enough values specified for <key name="MyKey"> 
^^^ identity constraint specified for element "persons".

The validation report is correct. I have two invalid "person" sub elements: #4 has a duplicate key value: [Bill,Gates,1955-10-28] and #5 has an incomplete key value: [Bill,Bush,] (missing "dob" component).

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



 PDF Printing Version