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.

This section covers the following topics:

Example of an Identity Constraint

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.

Diagram View

To specify an identity constraint:
1. Right-click the element for which you want to specify the identity constraint.
2. Select Add > and then Key, KeyRef, or Unique from the menu.
3. Right-click the new identity constraint, and select Selector.
4. In the Properties window, specify the XPath expression that identifies the set of elements to which the identity constraint applies.
5. Return to step 3 and select Field.
6. In the Properties window, specify the XPath expression that identifies the element or attribute for each element identified by the selector element that has to be unique.

Tree View

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.

To specify an identity constraint:
1. Click the element for which you want to specify the identity constraint.
2. In the XML Schema left-side tool bar, click .
3. In the drop-down list that Stylus Studio displays, double-click unique, key, or keyref.
4. In the Properties window, double-click the Name field and enter a name for the identity constraint.
5. If you selected keyref, then in the Properties window, double-click the Refer field and enter the name of the key definition.
6. In the tree representation, click the identity constraint you just defined.
7. In the left tool bar, click New Selector/Key .
8. In the drop-down list that Stylus Studio displays, double-click selector. You must define exactly one selector for each identity constraint.
9. In the Properties window, double-click the XPath Expression field and enter an XPath expression that returns the element for which you are specifying a constraint.
10. Click the unique, key, or keyref identity constraint you defined in Step 3.
11. In the left tool bar, click New Selector/Key .
12. In the drop-down list that Stylus Studio displays, double-click field. You must define one or more fields for each identity constraint. A field can be whatever the XPath expression (defined in the next step) retrieves.
13. In the Properties window, double-click the XPath Expression field and enter an XPath expression that returns the element or attribute that is the key or one of the keys for the constraint. XPath expressions associated with fields return the data that define the key for each element returned by the selector XPath expression.
14. Repeat step 10 through step 13 for each additional key field.
 
Free Stylus Studio XML Training: