DB2 Scalar functions - REC2XML
The REC2XML function returns a string formatted with XML tags, containing column names and column data.
The schema is SYSIBM.
In a Unicode database, if a supplied argument is a graphic string, it is first converted to a character string before the function is executed.
The decimal-constant value is used to calculate the result length of the function. For every column with a character data type, the length attribute of the column is multiplied by this expansion factor before it is added in to the result length.
To specify no expansion, use a value of 1.0. Specifying a value less than 1.0 reduces the calculated result length. If the actual length of the result string is greater than the calculated result length of the function, then an error is raised (SQLSTATE 22001).
The format-string is case-sensitive, so the following values must be specified in uppercase to be recognized.
Column names may or may not be valid XML attribute values. For column names which are not valid XML attribute values, character replacement is performed on the column name before it is included in the result string.
Column values may or may not be valid XML element names. If the format-string COLATTVAL is specified, then for the column names which are not valid XML element values, character replacement is performed on the column value before it is included in the result string. If the format-string COLATTVAL_XML is specified, then character replacement is not performed on column values (although character replacement is still performed on column names).
row
If a string of one or more blank characters is specified, then no beginning row-tag-string or ending row-tag-string (including the angle bracket delimiters) will appear in the result string.
The result of the function is VARCHAR in OCTETS, regardless of the string units of the environment or the specified columns. The maximum length is 32 672 bytes (SQLSTATE 54006).
Consider the following invocation:
REC2XML (dc, fs, rt, c1, c2, ..., cn)
If the value of fs is either COLATTVAL or COLATTVAL_XML, then the result is the same as this expression:
fs
COLATTVAL
COLATTVAL_XML
'<' CONCAT rt CONCAT '>' CONCAT y1 CONCAT y2 CONCAT ... CONCAT yn CONCAT '</' CONCAT rt CONCAT '>'
'<column name="' CONCAT xvcn CONCAT vn
'">' CONCAT rn CONCAT '</column>'
'" null="true"/>'
xvcn is equivalent to a string representation of the column name of cn, where any characters appearing in Table 2 are replaced with the corresponding representation. This ensures that the resulting string is a valid XML attribute or element value token.
The rn is equivalent to a string representation as indicated in Table 1
_XML
.
Character replacement:
Depending on the value specified for the format-string, certain characters in column names and column values will be replaced to ensure that the column names form valid XML attribute values and the column values form valid XML element values.
REC2XML does not insert blank spaces or new line characters in the output. All example output has been formatted to enhance readability.
SELECT REC2XML (1.0 , 'COLATTVAL' , '' , DEPTNO , MGRNO , ADMRDEPT) FROM DEPARTMENT WHERE DEPTNO = 'D01'
This example returns the following VARCHAR(117) string:
<row> <column name="DEPTNO">D01</column> <column name="MGRNO" null="true"/> <column name="ADMRDEPT">A00</column> </row>
&43<FIE
The length attribute for the REC2XML call with an expansion factor of 1.0 would be 128 (11 for the <row> and </row> overhead, 21 for the column names, 75 for the <column name=, >, </column> and double quotation marks, 7 for the CLASS_CODE data, 6 for the DAY data, and 8 for the STARTING data). Since the & and < characters will be replaced, an expansion factor of 1.0 will not be sufficient. The length attribute of the function will need to support an increase from 7 to 14 bytes for the new format CLASS_CODE data.
<row>
</row>
<column name=
>
</column>
&
<
However, since it is known that the DAY value will never be more than 1 digit long, an unused extra 5 units of length are added to the total. Therefore, the expansion only needs to handle an increase of 2. Since CLASS_CODE is the only character string column in the argument list, this is the only column data to which the expansion factor applies. To get an increase of 2 for the length, an expansion factor of 9/7 (approximately 1.2857) would be needed. An expansion factor of 1.3 will be used.
SELECT REC2XML (1.3 , 'COLATTVAL' , 'record' , CLASS_CODE , DAY , STARTING) FROM CL_SCHED WHERE CLASS_CODE = '&43<FIE'
<record> <column name="CLASS_CODE">&43<FIE</column> <column name="DAY">5</column> <column name="STARTING">06:45:00</column> </record>
SELECT REC2XML (1.0 , 'COLATTVAL_XML' , 'row' , EMPNO, RESUME_XML) FROM (SELECT EMPNO, CAST(RESUME AS VARCHAR(3500)) AS RESUME_XML FROM EMP_RESUME WHERE RESUME_FORMAT = 'XML') AS EMP_RESUME_XML
<row> <column name="EMPNO">{employee number}</column> <column name="RESUME_XML">{resume in XML}</column> </row>
{employee number}
{resume in XML}