XSD Tutorials - Herong's Tutorial Examples - Version 5.10, by Dr. Herong Yang

"keyref" Identity-Constraint XSD Example

This section provides a tutorial example on how to define a 'keyref' identity-constraint in an XSD schema to ensure key reference values are existing key values defined by another 'key' identify-constraint. Xecers2 XSD validator only reports a single generic error for multiple 'keyref' identity-constraint validation violations.

In order to provide key identify-constraint and key reference identify-constraint to keyref_identity.xml document presented in the previous section, I wrote this XSD schema, keyref_identity.xsd:

<?xml version="1.0" encoding="utf-8"?>
<!-- keyref_identity.xsd
 - Copyright (c) 2014, HerongYang.com, All Rights Reserved.
-->
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
 <xs:element name="survey">
  <xs:complexType>
   <xs:sequence>
    <xs:element name="questions" type="questionsType">
     <xs:key name="MyKey">
      <xs:selector xpath="./question"/>
      <xs:field xpath="@code"/>
     </xs:key>
    </xs:element>
    <xs:element name="answers" type="answersType">
     <xs:keyref name="MyKeyRef" refer="MyKey">
      <xs:selector xpath="./answer"/>
      <xs:field xpath="@qcode"/>
     </xs:keyref>
    </xs:element>
   </xs:sequence>
  </xs:complexType>  
 </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:complexType name="answersType">
  <xs:sequence>
   <xs:element name="answer" maxOccurs="unbounded">
    <xs:complexType>
     <xs:simpleContent>
      <xs:extension base="xs:string">
       <xs:attribute name="qcode" 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 to provide key values. The identify-constraint "MyKeyRef" is specified inside the "answers" element definition statement to reference key values. Let's use the "jaxp.TypeInfoWriter" sample program from Xerces2 to validate the XML document to see if these constraints work or not:

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

setDocumentLocator(systemId="file:///C:/herong/keyref_identity.xml...
startDocument()
 startElement(name="survey",type="#AnonType_survey",attributes={})
  startElement(name="questions",type="questionsType",attributes={})
   startElement(name="question",type="#AnonType_questionquestionsT...
   endElement(name="question")
   startElement(name="question",type="#AnonType_questionquestionsT...
   endElement(name="question")
   startElement(name="question",type="#AnonType_questionquestionsT...
   endElement(name="question")
   startElement(name="question",type="#AnonType_questionquestionsT...
   endElement(name="question")
  endElement(name="questions")
  startElement(name="answers",type="answersType",attributes={})
   startElement(name="answer",type="#AnonType_answeranswersType",a...
   endElement(name="answer")
   startElement(name="answer",type="#AnonType_answeranswersType",a...
   endElement(name="answer")
   startElement(name="answer",type="#AnonType_answeranswersType",a...
   endElement(name="answer")
   startElement(name="answer",type="#AnonType_answeranswersType",a...
   endElement(name="answer")
   startElement(name="answer",type="#AnonType_answeranswersType",a...
   endElement(name="answer")
   startElement(name="answer",type="#AnonType_answeranswersType",a...
   endElement(name="answer")
   startElement(name="answer",type="#AnonType_answeranswersType",a...
   endElement(name="answer")
[Error] keyref_identity.xml:20:11: Identity Constraint error: 
^^^ identity constraint "KeyRef@bbc1e0" has a keyref which refers to a
^^^ key or unique that is out of scope.
  endElement(name="answers")
 endElement(name="survey")
endDocument()

I think the "MyKey" and "MyKeyRef" constraints worked ok, and here is what happened:

  • When the parser reached the "questions" element, it started to apply the "MyKey" constraint.
  • All sub elements have the required and unique "question[@code]" attributes. So the "MyKey" constraint validation passed.
  • When the parser reached the "answers" element, it started to apply the "MyKeyRef" constraint.
  • The first sub element <answer qcode="Java">Java is an island of Indonesia.</answer> passed the validation, because qcode="Java" refers to an existing key value.
  • The second sub element <answer qcode="Java">Java is programming language.</answer> passed the validation, because qcode="Java" refers to the same existing key value as the first sub element.
  • The third sub element <answer qcode="John">John is a character in Atlas Shrugged.</answer> passed the validation, because qcode="John" refers to another existing key value.
  • The forth sub element <answer qcode="Jojo">Jojo is a singer-songwriter.</answer> should fail the validation, because qcode="Jojo" refers to a key value that does not exist. The parser seemed to be holding this error and printed out the error message later.
  • The fifth sub element <answer qcode="Jim1">Jim is in Java island.</answer> passed the validation, because qcode="Jim1" refers to another existing key value.
  • The sixth sub element <answer qcode="Jim2">Jim is having a coffee.</answer> passed the validation, because qcode="Jim2" refers to another existing key value.
  • The seventh sub element <answer>These are good questions.</answer> should fail the validation, because qcode is not provided at all. The parser seemed to be holding this error and printed out the error message later.
  • After parsing all sub elements of "answers", the parser final printed out a single generic error message covering both error instances.

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

"keyref" Identity-Constraint XSD Example - Updated in 2014, by Dr. Herong Yang