XSD Tutorials - Herong's Tutorial Examples - v5.22, by 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) 2002-2013 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:
herong> java_xerces 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:
Table of Contents
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
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
►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