Note: The auto-compute doesn't work for the custom attributes, added in <objectname>Extra section. Instead to use an attribute, add it as an element.
Automatically computed attributes are specified by:
1.Defining an XSL transformation associated with the object type that is used to compute the values
2.Marking which object attributes are automatically computed.
The XSL transformation acts on the object being updated to generate a new ‘temporary’ object. The value of the attributes marked as automatically computed are copied from the corresponding attributes of this temporary object.
The XSL transformation is defined using the GN4-specific extension element ‘compute’ that is placed inside the ‘appInfo’ element of the complexType element defining the object type.
Automatically computed attributes are marked using the GN4-specific extension attribute ‘compute’, which applies to the ‘attribute’ and ‘element’ elements in the schema defining the object attribute. The value of the ‘compute’ attribute is a Boolean - ‘true’ indicates that the corresponding object attribute is automatically computed, ‘false’ indicates that it is a normal attribute. The default value is ‘false’.
<xs:element name="person">
<xs:complexType>
<xs:annotation>
<xs:appinfo>
<gs:compute>
<xsl:stylesheet
version='1.0'
xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>
<xsl:template match='/*'>
<person
<xsl:attribute name="fullName">
<xsl:value-of
select="concat(@firsName,’ ’,@lastName)"/>
</xsl:attribute>
</person>
</xsl:template>
</xsl:stylesheet>
</gs:compute>
</xs:appinfo>
</xs:annotation>
<xs:complexContent>
<xs:extension base="object">
<xs:attribute
name="firstName"
type="xs:string"/>
<xs:attribute
name="lastName"
type="xs:string"/>
<xs:attribute
name="fullName"
type="xs:string"
gs:compute="true"/>
</xs:extension>
</xs:complexContent>
</xs:complexType>
</xs:element>
The example above defines a ‘person’ object type that has two normal string attributes,‘firstName’ and ‘lastName’, and a third computed string attribute ‘fullName’ automatically computed by concatenating the first two attribute with a space in the middle. Importing a person object defined by:
<person
firstName="John"
lastName="Smith"/>
automatically creates a database object described by:
<person
firstName="John"
lastName="Smith"
fullName="John Smith"/>
IMPORTANT: When importing data automatically, values for computed attributes are ignored and always computed according to the formula, so that in the preceding example importing a person definition like:
<person
firstName="John"
lastName="Smith"
fullName="John F. Smith"/>
will not change the value of ‘fullName’ - it will still be ‘John Smith’.
Any attribute can be automatically computed, regardless of its type. The only exception is the special ‘id’ attribute that is always system-generated .
Automatically computed attributes can be used as part of the object’s key and descriptive/short names.
The XSL transformation associated with the object type must produce a valid XML representation of a single object of the type itself or of one of its ancestor types. In the example above, note that the XSL transformation produces a ‘person’ element – that is, the XML representation of a ‘person’ object according to the schema we are defining. A transformation producing an ‘object’ element would have been valid as well (‘object’ being the direct ancestor of ‘person’) – but it could have been used only to compute attributes belonging to ‘object’ and not just to ‘person’ like fullName.
See also
Overriding/inheriting auto-computed attributes