The db2-xdb:expression annotation specifies a customized expression, the result of which is inserted into the table this element is mapped to.
db2-xdb:expression belongs to the set of decomposition annotations that can be added to an XML schema document to describe the mappings between elements and attributes of XML documents to DB2® base tables. The decomposition process uses the annotated XML schema to determine how elements and attributes of an XML document should be decomposed into DB2 tables.
Attribute of <xs:element> or <xs:attribute>, or optional child element of <db2-xdb:rowSetMapping>, effective only on annotations that include a column mapping
<db2-xdb:rowSetMapping>
<db2-xdb:rowSet>value</db2-xdb:rowSet>
<db2-xdb:column>value</db2-xdb:column>
<db2-xdb:expression>value</db2-xdb:expression>
…
</db2-xdb:rowSetMapping>
http://www.ibm.com/xmlns/prod/db2/xdb1
expression := function (arglist) | constant | $DECOMP_CONTENT | $DECOMP_ELEMENTID |
$DECOMP_DOCUMENTID | (scalar-fullselect) | expression operator expression |
(expression) | special-register | CAST (expression AS data-type) |
XMLCAST (expression AS data-type) | XML-function
operator := + | - | * | / | CONCAT
arglist := expression | arglist, expression
The db2-xdb:expression annotation enables you to specify a customized expression, which is applied to the content of the XML element or attribute being annotated when $DECOMP_CONTENT is used. The result of evaluating this expression is then inserted into the column specified during decomposition.
This annotation is also useful in cases where you want to insert constant values (such as the name of an element), or generated values that do not appear in the document.
The following example shows how the db2-xdb:expression annotation can be used to apply a value from the XML document to a user-defined function. The result returned from the UDF is then inserted into the database, rather than the value from the document itself. A section of the annotated schema is presented first.
<xs:element name="author">
<xs:complexType>
<xs:sequence>
<xs:element name="firstname" type="xs:string" />
<xs:element name="lastname" type="xs:string" />
<xs:element name="activeStatus" type="xs:boolean" />
<xs:attribute name="ID" type="xs:integer"
db2-xdb:rowSet="AUTHORS" db2-xdb:column="NUMBOOKS"
db2-xdb:expression="AuthNumBooks (INTEGER ($DECOMP_CONTENT))" />
</xs:sequence>
</xs:complexType>
</xs:element>
Assume that there is a user-defined function called AuthNumBooks that takes an integer parameter, which represents the author's ID, and returns the total number of books that author has in the system.
<author ID="22">
<firstname>Ann</firstname>
<lastname>Brown</lastname>
<activeStatus>1</activeStatus>
</author>
$DECOMP_CONTENT is replaced with the value "22" from the instance of the ID attribute. Because $DECOMP_CONTENT is always substituted with a character type, and because the AuthNumBooks UDF takes an integer parameter, the db2-xdb:expression annotation must cast $DECOMP_CONTENT to an integer. Assume that the UDF returns the integer 8 for this author whose ID is 22; 8 is then inserted into the NUMBOOKS column of the AUTHORS table, as shown next.
AUTHID | FIRSTNAME | SURNAME | ACTIVE | NUMBOOKS |
---|---|---|---|---|
NULL | NULL | NULL | NULL | 8 |