>>-REC2XML--(--decimal-constant--,--format-string---------------> .----------------. V | >--,--row-tag-string----,--column-name-+--)--------------------><
The schema is SYSIBM.
The REC2XML function returns a string formatted with XML tags, containing column names and column data. 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.
>>-<--row-tag-string-->-----------------------------------------> .----------------------------------------------------------------------. V | >----<--column-name--=--"column-name"--+->--column-value--</--column-->-+-+--> '-null="true"--/>----------------' >--</--row-tag-string-->---------------------------------------><
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).
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. The maximum length is 32 672 bytes (SQLSTATE 54006).
REC2XML (dc, fs, rt, c1, c2, ..., cn)
'<' CONCAT rt CONCAT '>' CONCAT y1 CONCAT y2
CONCAT ... CONCAT yn CONCAT '</' CONCAT rt CONCAT '>'
where
yn is equivalent to: '<column name="' CONCAT xvcn CONCAT vn
and
vn is equivalent to: '">' CONCAT rn CONCAT '</column>'
if
the column is not null, and '" null="true"/>'
if
the column value is null.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.
Data type of cn | rn |
---|---|
CHAR, VARCHAR | The value is a string. If the format-string does not end in the characters "_XML", then each character in cn is replaced with the corresponding replacement representation from Table 2, as indicated. The length attribute is: dc * the length attribute of cn. |
SMALLINT, INTEGER, BIGINT, DECIMAL, NUMERIC, REAL, DOUBLE | The value is LTRIM(RTRIM(CHAR(cn))). The length attribute is the result length of CHAR(cn). The decimal character is always the period ('.') character. |
DATE | The value is CHAR(cn,ISO). The length attribute is the result length of CHAR(cn,ISO). |
TIME | The value is CHAR(cn,JIS). The length attribute is the result length of CHAR(cn,JIS) |
TIMESTAMP | The value is CHAR(cn). The length attribute is the result length of CHAR(cn). |
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.
Character | Replacement |
---|---|
< | < |
> | > |
" | " |
& | & |
' | ' |
Examples:
SELECT REC2XML (1.0, 'COLATTVAL', '', DEPTNO, MGRNO, ADMRDEPT)
FROM DEPARTMENT
WHERE DEPTNO = 'D01'
<row>
<column name="DEPTNO">D01</column>
<column name="MGRNO" null="true"/>
<column name="ADMRDEPT">A00</column>
</row>
The length attribute for the REC2XML call (see below) 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 quotes, 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.
SELECT REC2XML (1.3, 'COLATTVAL', 'record', CLASS_CODE, DAY, STARTING)
FROM CL_SCHED
WHERE CLASS_CODE = '&43<FIE'
This example
returns the following VARCHAR(167) string: <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
This example returns a row
for each employee who has a resume in XML format. Each returned row
will be a string with the following format: <row>
<column name="EMPNO">{employee number}</column>
<column name="RESUME_XML">{resume in XML}</column>
</row>
Where "{employee number}" is the actual
EMPNO value for the column and "{resume in XML}" is the actual XML
fragment string value that is the resume.