This section describes the 'key' identity-constraint, where a combination of certain sub elements or attributes is defined as a key identity, which must have values and values must be unique.
What Is "key" Identity-Constraint?
A "key" Identity-Constraint is a constraint on an XML element, where
a combination of certain sub elements or attributes is defined as a key identity,
which must have values and values must be unique.
"key" identity-constraint in XML document is very similar to the concept of
PRIMARY KEY constraint on multiple columns in a database table.
Here is the syntax on how a "key" identity-constraint should be defined
in XSD schema:
The "key" declaration must be placed inside the "element" statement
which declares the parent element where the constraint will be applied.
"key[@name]" is required to provide a name for this key identity constraint.
"selector" is required to specify a relative XPath to match a sequence of sub elements
from which identity values will be constructed and validated.
At least 1 "field" is required.
Each "field" specifies a relative XPath pointing to an attribute or a simple element content,
which will be used as part of the identity construction.
For example, I have the following XML document, key_identity.xml:
<?xml version="1.0" encoding="utf-8"?>
<!-- key_identity.xml
- Copyright (c) 2014, HerongYang.com, All Rights Reserved.
-->
<questions>
<question>What is Java?</question>
<question code="John">Who is John?</question>
<question code="Jim">Where is Jim?</question>
<question code="Jim">What is Jim doing?</question>
</questions>
For this XML document, I want to have an XSD schema
that has an identity-constraint to ensure values in "code" attributes
can be used as a key identity.
Here is my XSD schema, key_identity.xsd:
The identify-constraint "MyKey" is specified inside the
"questions" element definition statement.
Let's use the "jaxp.TypeInfoWriter" sample program from Xerces2
to validate the XML document to see if the constraint works or not:
>jdk8x2r jaxp.TypeInfoWriter -a key_identity.xsd -i key_identity.xml
setDocumentLocator(systemId="file:///C:/herong/key_identity.xml", ...
startDocument()
startElement(name="questions",type="questionsType",attributes={})
startElement(name="question",type="#AnonType_questionquestionsTy...
[Error] key_identity.xml:6:37: cvc-identity-constraint.4.2.1.a: Element
^^^ "questions" has no value for the key "MyKey".
endElement(name="question")
startElement(name="question",type="#AnonType_questionquestionsTy...
endElement(name="question")
startElement(name="question",type="#AnonType_questionquestionsTy...
endElement(name="question")
[Error] key_identity.xml:9:23: cvc-identity-constraint.4.1: Duplicate
^^^ unique value [Jim] declared for identity constraint
^^^ "MyKey" of element "questions".
startElement(name="question",type="#AnonType_questionquestionsTy...
endElement(name="question")
endElement(name="questions")
endDocument()
I think the "MyKey" constraint worked correctly,
and here is what happened:
When the parser reached the "questions" element,
it started to apply the "MyKey" constraint.
The first sub element <question>What is Java?</question>
matched the "selector" condition, xpath="./question".
The parser tried to get the "field" value using xpath="@code".
In the case, it was a missing value. Validation failed.
The second sub element <question code="John">Who is John?</question>
matched the "selector" condition, xpath="./question".
The parser tried to get the "field" value using xpath="@code".
In the case, it was a unique value, "John". Validation passed.
The third sub element <question code="Jim">Where is Jim?</question>
matched the "selector" condition, xpath="./question".
The parser tried to get the "field" value using xpath="@code".
In the case, it was a duplicate value, "Jim",
because the next sub element also has the same attribute code="Jim".
Validation failed.
2 validation error messages in the output confirm that my above analysis is correct.