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:
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.