This section provides a tutorial example on how to specify a 'key' identify constraint in an XSD schema that uses multiple 'field' to construct identify values with multiple components.
Identify-constraints, "unique", "key", and "keyref" can support identities that use multiple fields.
Here is a sample XML document, key_identity_3_fields.xml:
For this XML document, I want to have an XSD schema
that has an identity-constraint to ensure the combination "fname", "lname" and "dob" attribute values
become a key identity.
Here is my XSD schema, key_identity_3_fields.xsd:
The identify-constraint "MyKey" is specified inside the
"persons" element definition statement.
Notice that 3 "field" statements are used to build the identity with multiple data components.
Let's use the "jaxp.TypeInfoWriter" sample program from Xerces2
to validate the XML document to see if this constraint works or not:
>jdk8x2r jaxp.TypeInfoWriter -a key_identity_3_fields.xsd
^^^ -i key_identity_3_fields.xml
setDocumentLocator(systemId="file:///C:/herong/key_identity_3_fiel...
startDocument()
startElement(name="persons",type="personsType",attributes={})
startElement(name="person",type="#AnonType_personpersonsType",at...
endElement(name="person")
startElement(name="person",type="#AnonType_personpersonsType",at...
endElement(name="person")
startElement(name="person",type="#AnonType_personpersonsType",at...
endElement(name="person")
[Error] key_identity_3_fields.xml:9:55: cvc-identity-constraint.4.2.2:
^^^ Duplicate key value [Bill,Gates,1955-10-28] declared for identity
^^^ constraint "MyKey" of element "persons".
startElement(name="person",type="#AnonType_personpersonsType",at...
endElement(name="person")
startElement(name="person",type="#AnonType_personpersonsType",at...
[Error] key_identity_3_fields.xml:10:37: cvc-identity-constraint
^^^ .4.2.1.b: Not enough values specified for <key name="MyKey">
^^^ identity constraint specified for element "persons".
endElement(name="person")
endElement(name="persons")
endDocument()
The validation report is correct.
I have two invalid "person" sub elements:
#4 has a duplicate key value: [Bill,Gates,1955-10-28]
and #5 has an incomplete key value: [Bill,Bush,] (missing "dob" component).