XSD Tutorials - Herong's Tutorial Examples - v5.22, by Herong Yang
Identity-Constraint with Multiple Fields
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:
<?xml version="1.0" encoding="utf-8"?> <!-- key_identity_3_fields.xml - Copyright (c) 2002-2013 HerongYang.com. All Rights Reserved. --> <persons> <person fname="Bill" lname="Gates" dob="1955-10-28"/> <person fname="Joe" lname="Gates" dob="1955-10-28"/> <person fname="Joe" lname="Bush" dob="1955-04-19"/> <person fname="Bill" lname="Gates" dob="1955-10-28"/> <person fname="Bill" lname="Bush"/> </persons>
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:
<?xml version="1.0" encoding="utf-8"?> <!-- key_identity_3_fields.xsd - Copyright (c) 2002-2013 HerongYang.com. All Rights Reserved. --> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:element name="persons" type="personsType"> <xs:key name="MyKey"> <xs:selector xpath="./person"/> <xs:field xpath="@fname"/> <xs:field xpath="@lname"/> <xs:field xpath="@dob"/> </xs:key> </xs:element> <xs:complexType name="personsType"> <xs:sequence> <xs:element name="person" maxOccurs="unbounded"> <xs:complexType> <xs:simpleContent> <xs:extension base="xs:string"> <xs:attribute name="fname" type="xs:string"/> <xs:attribute name="lname" type="xs:string"/> <xs:attribute name="dob" 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 "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:
herong> java_xerces 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).
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