|
Home >Online Product Documentation >Table of Contents >Adding an Identity Constraint to an Element Adding an Identity Constraint to an ElementXML 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.
l A 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.
l A 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.
l A 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: Example of an Identity ConstraintThis 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 ViewThis 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.
|