The list of attributes that make up the object’s key is defined using the GN4-specific extension attribute ‘key’ that applies to complexType elements. It lists the name of the key attributes separated by spaces, e.g.:
<xs:element name="test">
<xs:complexType gs:key="location">
<xs:complexContent>
<xs:extension base="object">
<xs:attribute
name="location"
type="xs:string"/>
</xs:extension>
</xs:complexContent>
</xs:complexType>
</xs:element>
This defines the single attribute ‘location’ as the key for objects of type ‘test’.
A more complex variation:
<xs:element name="test">
<xs:complexType gs:key="name location">
<xs:complexContent>
<xs:extension base="object">
<xs:attribute
name="location"
type="xs:string"/>
</xs:extension>
</xs:complexContent>
</xs:complexType>
</xs:element>
defines the pair of attributes ‘name’ and ‘location’ – in this order – as the key for objects of type ‘test’. Note that the ‘name’ attribute is inherited from the ancestor type ‘object’ – but it is still legal in the key definition.
Key definitions can be inherited:
<xs:complexType name="object" gs:key="name">
<xs:attribute name="id" type="xs:ID" />
<xs:attribute name="name" type="tName" />
</xs:complexType>
<xs:element name="test">
<xs:complexType>
<xs:complexContent>
<xs:extension base="object">
<xs:attribute
name="location"
type="xs:string"/>
</xs:extension>
</xs:complexContent>
</xs:complexType>
</xs:element>
The example above defines the single attribute ‘name’ as the default key for the ‘object’ type – so the key for objects of type ‘test’ (that in this case do not define a separate key) will be the single attribute ‘name’.
Note that the key definition applies to the object type where it is defined – i.e., it enforces the uniqueness of objects of that type and all its descendants that do not define a separate key. In the last example, the attribute ‘name’ will be unique amongst all the objects whose type descend from the type ‘object’ and do not define another key.
Only attributes with a simple value (string – except when they are stored as unlimited length text in the database, integer, etc.) and simple references (to only one object) can be used as part of the key. All other attributes, such as, the special ‘id’ attribute, multiple references, privileges, access permissions, etc., cannot be used as key. Automatically computed attributes can be used as part of the key.
Note that the definition of a key is compulsory: every object type must have one – either defined directly or inherited.
See also