This section describes the 'unique' identity-constraint, where a combination of certain sub elements or attributes is defined as an identity, which must have unique values. Missing values are acceptable.
What Is "unique" Identity-Constraint?
A "unique" Identity-Constraint is a constraint on an XML element, where
a combination of certain sub elements or attributes is defined as an identity,
which must have unique values. Missing values are acceptable.
"unique" identity-constraint in XML document is very similar to the concept of
UNIQUE constraint on multiple columns in a database table.
Here is the syntax on how a "unique" identity-constraint should be defined
in XSD schema:
The "unique" declaration must be placed inside the "element" statement
which declares the parent element where the constraint will be applied.
"unique[@name]" is required to provide a name for this identity constraint.
"selector" is required to specify a relative XPath to match a sequence of sub elements
from which identity values 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, unique_identify.xml:
<?xml version="1.0" encoding="utf-8"?>
<!-- unique_identify.xml
- Copyright (c) 2014, HerongYang.com, All Rights Reserved.
-->
<questions>
<question>What is Java?</question>
<question name="John">Who is John?</question>
<question name="Jim">Where is Jim?</question>
<question name="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 "name" attributes are unique.
Here is my XSD schema, unique_identify.xsd:
The identify-constraint "MyUniqueness" 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 unique_identify.xsd
^^^ -i unique_identify.xml
setDocumentLocator(systemId="file:///C:/herong/unique_identify.xml...
startDocument()
startElement(name="questions",type="questionsType",attributes={})
startElement(name="question",type="#AnonType_questionquestionsTy...
endElement(name="question")
startElement(name="question",type="#AnonType_questionquestionsTy...
endElement(name="question")
startElement(name="question",type="#AnonType_questionquestionsTy...
endElement(name="question")
[Error] unique_identify.xml:9:23: cvc-identity-constraint.4.1:
^^^ Duplicate unique value [Jim] declared for identity constraint
^^^ "MyUniqueness" of element "questions".
startElement(name="question",type="#AnonType_questionquestionsTy...
endElement(name="question")
endElement(name="questions")
endDocument()
I think the "MyUniqueness" constraint worked correctly,
and here is what happened:
When the parser reached the "questions" element,
it started to apply the "MyUniqueness" 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="@name".
In the case, it was a missing value. Validation passed.
The second sub element <question name="John">Who is John?</question>
matched the "selector" condition, xpath="./question".
The parser tried to get the "field" value using xpath="@name".
In the case, it was a unique value, "John". Validation passed.
The third sub element <question name="Jim">Where is Jim?</question>
matched the "selector" condition, xpath="./question".
The parser tried to get the "field" value using xpath="@name".
In the case, it was a duplicate value, "Jim",
because the next sub element also has the same attribute name="Jim".
Validation failed.
The validation error message in the output confirms that my above analysis is correct.