Home >Online Product Documentation >Table of Contents >Adding an Identity Constraint to an Element
XML Schemas provide a feature that is similar to the DTD ID identity constraint. In a DTD, the value of an ID attribute must be unique within an XML document. In XML Schemas, the type of an identity constraint can be unique, key, or keyref. You use XPath expressions to define the scope of the constraint.
You associate an identity constraint with an element.
unique identity constraint forces the result of evaluation of an XPath expression to be unique. Stylus Studio evaluates the XPath expression against the element for which you define the identity constraint. If the element is present, the result must be unique among the children of that element. key identity constraint specifies that the fields that form the expression must be present in all instance documents. For example, if a key is based on date and number attributes, the date and number attributes must always be specified. keyref identity constraint is equivalent to the IDREF attribute in DTDs. It specifies that the contents of a field in the instance document is the value of a key that is defined in another document. For example, a Quote document would have a reference to the RFQ that originated it. This section covers the following topics:
This topic provides an example of an element with an identity constraint. Introductory information about identity constraints is in Adding an Identity Constraint to an Element. See also Tree View.
Suppose you define the following element in an XML Schema:
In an XML document that uses this schema, you could define the following elements:
If you want to enforce that there is just one part element for each product that has been purchased, you add the following to the previous XML Schema example:
The schema validator starts with an initial context set that contains purchaseReport elements. It runs the XPath expression parts/part to obtain the data set to be checked. In this example, this is the two part elements. The schema validator then gathers the values from the number and vendor attributes, and builds a key from these values. It then uses the key to check that there are no part elements that have the same tuple.
selector element that has to be unique.
This topic provides the procedure for specifying an element with an identity constraint. Introductory information about identity constraints is in Adding an Identity Constraint to an Element. See also Example of an Identity Constraint.
selector for each identity constraint.
fields for each identity constraint. A field can be whatever the XPath expression (defined in the next step) retrieves.
selector XPath expression.