
The XMLSERIALIZE function returns a serialized XML value of the specified data type generated from the XML-expression argument.
.-CONTENT-. >>-XMLSERIALIZE--(--+---------+--XML-expression--AS--| data-type |--> .--------------------------------------. V (1).-VERSION--'1.0'---------------. | >--------+------------------------------+-+--)----------------->< | .-EXCLUDING XMLDECLARATION-. | '-+-INCLUDING XMLDECLARATION-+-' data-type .-(--1--)-------. |--+-+-+-+-CHARACTER-+--+---------------+----------+--+----------------+---------------------+------------+--| | | | '-CHAR------' '-(--integer--)-' | +-FOR BIT DATA---+ | | | | '-+-+-CHARACTER-+--VARYING-+--(--integer--)-' +-FOR SBCS DATA--+ | | | | | '-CHAR------' | +-FOR MIXED DATA-+ | | | | '-VARCHAR----------------' '-ccsid-clause---' | | | | .-(--1M--)-------------. | | | '-----+-+-CHARACTER-+--LARGE OBJECT-+------+----------------------+--+----------------+-' | | | '-CHAR------' | '-(--integer--+---+--)-' +-FOR SBCS DATA--+ | | '-CLOB------------------------' +-K-+ +-FOR MIXED DATA-+ | | +-M-+ '-ccsid-clause---' | | '-G-' | | .-(--1--)-------. | +-+---GRAPHIC----+---------------+-------+--+--------------+-------------------------------------------+ | | '-(--integer--)-' | '-ccsid-clause-' | | +-+-GRAPHIC VARYING-+--(--integer--)---+ | | | '-VARGRAPHIC------' | | | | .-(--1M--)-------------. | | | '---DBCLOB----+----------------------+-' | | '-(--integer--+---+--)-' | | +-K-+ | | +-M-+ | | '-G-' | | .-(--1--)-------. | +-+-+-+-NATIONAL CHARACTER-+--+---------------+----------+---------------------+--+------------------+-+ | | | +-NATIONAL CHAR------+ '-(--integer--)-' | | '-normalize-clause-' | | | | '-NCHAR--------------' | | | | | '-+-+-NATIONAL CHARACTER-+--VARYING-+--(--integer--)-' | | | | | +-NATIONAL CHAR------+ | | | | | | '-NCHAR--------------' | | | | | '-NVARCHAR------------------------' | | | | .-(--1M--)-------------. | | | '-----+-+-NATIONAL CHARACTER-+--LARGE OBJECT-+------+----------------------+-' | | | '-NCHAR--------------' | '-(--integer--+---+--)-' | | '-NCLOB--------------------------------' +-K-+ | | +-M-+ | | '-G-' | | .-(--1--)-------. | '-+-+-BINARY--+---------------+---------+-----------------+--------------------------------------------' | | '-(--integer--)-' | | | '-+-BINARY VARYING-+--(--integer--)-' | | '-VARBINARY------' | | .-(--1M--)-------------. | '---+-BLOB----------------+----+----------------------+-' '-BINARY LARGE OBJECT-' '-(--integer--+---+--)-' +-K-+ +-M-+ '-G-'
ccsid-clause |--CCSID--integer--+------------------+-------------------------| '-normalize-clause-' normalize-clause .-NOT NORMALIZED-. |--+----------------+-------------------------------------------| '-NORMALIZED-----'
If a CCSID is specified and the data-type is GRAPHIC, VARGRAPHIC, or DBCLOB, the CCSID must be a Unicode CCSID.
If the CCSID attribute is not specified, the CCSID is
determined as described in CAST specification.
If the result of XML-expression can be null, the result can be null; if the result of XML-expression is null, the result is the null value.
Example 1: Serialize into CLOB of UTF-8, the XML value that is returned by the XMLELEMENT function, which is a simple XML element with "Emp" as the element name and an employee name as the element content:
SELECT e.id, XMLSERIALIZE(XMLELEMENT(NAME "Emp",
e.fname || ' ' ||e.lname)
AS CLOB(100) CCSID 1208) AS "result"
FROM employees e
The result looks similar
to the following results:ID result ----- --------------------- 1001 <Emp>John Smith</Emp> 1206 <Emp>Mary Martin</Emp>
Example 2: Serialize into a string of BLOB type, the XML value that is returned by the XMLELEMENT function:
SELECT XMLSERIALIZE(XMLELEMENT(NAME "Emp",
e.fname || ' ' ||e.lname)
AS BLOB(1K)
VERSION '1.0') AS "result"
FROM employees e WHERE e.id = '1001'
The
result looks similar to the following results:result --------------------- <Emp>John Smith</Emp>
Example 3: Serialize into a string of CLOB type, the XML value that is returned by the XMLELEMENT function. Include the XMLDECLARATION:
SELECT e.empno, e.firstnme, e.lastname,
XMLSERIALIZE(XMLELEMENT(NAME "xmp:Emp",
XMLNAMESPACES('http://www.xmp.com' as "xmp"),
XMLATTRIBUTES(e.empno as "serial"),
e.firstnme, e.lastname
OPTION NULL ON NULL))
AS CLOB(1000) CCSID 1208
INCLUDING XMLDECLARATION) AS "Result"
FROM employees e WHERE e.empno = 'A0001'
The
result looks similar to the following results: EMPNO FIRSTNME LASTNAME Result
------ --------- --------- -----------------------
A0001 John Smith <?xml version="1.0" encoding="UTF-8" ?>
<xmp:Emp xmlns:xml="http://www.xmp.com"
serial="A0001">JohnParker</xmp:Emp>
