Keys

The discussion in the previous section concerning unique values is of relevance to this problem. The target object, or 'keyed' object, must be unique, so it should not be surprising that the Key element has the same content model and attributes as the Unique element (described above). The Selector sub-element again identifies the elements to be grouped and identified within a particular 'namespace', and the Field sub-element again identifies the key value or values. In addition, the Name attribute must be used to give the key an identifier that can be referred to later:

<element name="employees">
  ...
  <key name="PersonKeys">
    <selector xpath="person" />
    <field xpath="employeeNum" />
  </key>
</element>


   <employees>
     <person>
       <employeeNum>1234</employeeNum><name>J Smith</name>
     </person>
     <person>
       <employeeNum>9876</employeeNum><name>P Jones</name>
     </person>
   </employees>

Key references

A reference can only be made to a key if the referencing object holds a copy of the key value or values. To refer to one of the objects in the example above, it must already have an employee number, such as '1234'.

The Keyref element has an equivalent structure to the Key element, as it requires the Selector and Field elements to identify the referring object, and its copy of the key value. But it also has a Refer attribute, which is used to refer to the key name:

<element name="department">
  ...
  <keyref name="PersonKey">
    <selector xpath="person" />
    <field xpath="@employeeNum" refer="PersonKeys" />
  </keyref>
</element>


   <department>
     <person employeeNum="1234" />
     <person employeeNum="9876" />
   </department>

Note that the values do not have to be held in the same form. In the example above, the employee number is element content in the key objects, and attribute value content in the referencing objects. However, the values in each place must belong to the same data type, such as 'string' or 'integer'. Also, when multiple Field elements are needed, they must be arranged in the same order, though the actual values may be present in a different order within the two structures.

A reference can also be made to Unique elements, which also have a Name attribute. This option is needed to cater for the possibility that some of the target structures have no key values. Note that the Keyref element itself also has a Name attribute, but it is not clear what purpose it serves (nothing references this element).

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.117.137.64