What Is "keyref" Identity-Constraint?

This section describes the 'keyref' identity-constraint, where a combination of certain sub elements or attributes is defined as a key reference identity, which must have values that reference to an existing key identity.

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

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

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

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

Note that:

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

<?xml version="1.0" encoding="utf-8"?>
<!-- keyref_identity.xml
 - Copyright (c) 2002-2013 HerongYang.com. All Rights Reserved.
-->
<survey>
<questions>
 <question code="Java">What is Java?</question>
 <question code="John">Who is John?</question>
 <question code="Jim1">Where is Jim?</question>
 <question code="Jim2">What is Jim doing?</question>
</questions>
<answers>
 <answer qcode="Java">Java is an island of Indonesia.</answer>
 <answer qcode="Java">Java is programming language.</answer>
 <answer qcode="John">John is a character in Atlas Shrugged.</answer>
 <answer qcode="Jojo">Jojo is a singer-songwriter.</answer>
 <answer qcode="Jim1">Jim is in Java island.</answer>
 <answer qcode="Jim2">Jim is having a coffee.</answer>
 <answer>These are good questions.</answer>
</answers>
</survey>

For this XML document, I want to have an XSD schema that has two identity-constraints to ensure values in "question[@code]" attributes can be used as a key identity. And values in "answer[@qcode]" attributes are referring to an existing value in "question[@code]" attribute.

See next section for the XSD schema example and validation result.

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 APIs

 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

 Archived Tutorials

 References

 Full Version in PDF/EPUB