DB2 Scalar functions - XMLELEMENT
The XMLELEMENT function returns an XML value that is an XQuery element node.
The schema is SYSIBM. The function name cannot be specified as a qualified name.
If xmlnamespaces-declaration is not specified, namespace declarations are not associated with the constructed element.
If element-content-expression is not specified, an empty string is used as the content for the element and OPTION NULL ON NULL or EMPTY ON NULL must not be specified.
This function takes an element name, an optional collection of namespace declarations, an optional collection of attributes, and zero or more arguments that make up the content of the XML element. The result is an XML sequence containing an XML element node or the null value.
The data type of the result is XML. If any of the element-content-expression arguments can be null, the result can be null; if all the element-content-expression argument values are null and the NULL ON NULL option is in effect, the result is the null value.
Casting between data types
VALUES XMLELEMENT( NAME "c", XMLQUERY( 'declare namespace ipo="www.ipo.com"; $m/ipo:a/@ipo:b' PASSING XMLPARSE( DOCUMENT '<tst:a xmlns:tst="www.ipo.com" tst:b="2"/>' ) AS "m" ) )
<c xmlns:tst="www.ipo.com" tst:b="2"/>
VALUES XMLELEMENT( NAME "tst:c", XMLNAMESPACES( 'www.tst.com' AS "tst" ), XMLQUERY( 'declare namespace ipo="www.ipo.com"; $m/ipo:a/@ipo:b' PASSING XMLPARSE( DOCUMENT '<tst:a xmlns:tst="www.ipo.com" tst:b="2"/>' ) AS "m" ) )
<tst:c xmlns:tst="www.tst.com" xmlns:db2ns-a1="www.ipo.com" db2ns-a1:b="2"/>
Note: XMLELEMENT does not insert blank spaces or new line characters in the output. All example output has been formatted to enhance readability.
SELECT E.FIRSTNME, E.LASTNAME, XMLELEMENT( NAME "Emp", XMLELEMENT( NAME "firstname", E.FIRSTNME ), XMLELEMENT( NAME "lastname", E.LASTNAME ) OPTION NULL ON NULL ) AS "Result" FROM EMPLOYEE E WHERE E.EDLEVEL = 12
FIRSTNME LASTNAME Emp JOHN PARKER <Emp><firstname>JOHN</firstname> <lastname>PARKER</lastname></Emp> MAUDE SETRIGHT <Emp><firstname>MAUDE</firstname> <lastname>SETRIGHT</lastname></Emp> MICHELLE SPRINGER <Emp><firstname>MICHELLE</firstname> <lastname>SPRINGER</lastname></Emp>
SELECT XMLELEMENT( NAME "Department", XMLATTRIBUTES( E.WORKDEPT AS "name" ), XMLAGG( XMLELEMENT( NAME "emp", E.FIRSTNME ) ORDER BY E.FIRSTNME ) ) AS "dept_list" FROM EMPLOYEE E WHERE E.WORKDEPT IN ('A00', 'B01') GROUP BY WORKDEPT
dept_list <Department name="A00"> <emp>CHRISTINE</emp> <emp>SEAN</emp> <emp>VINCENZO</emp> </Department> <Department name="B01"> <emp>MICHAEL</emp> </Department>
SELECT XMLELEMENT( NAME "root", XMLNAMESPACES(DEFAULT 'http://mytest.uri'), XMLATTRIBUTES(cid), (SELECT XMLAGG( XMLELEMENT( NAME "poid", poid ) ) FROM purchaseorder WHERE purchaseorder.custid = customer.cid ) ) FROM customer WHERE cid = '1002'
<root xmlns="http://mytest.uri" CID="1002"> <poid>5000</poid> <poid>5003</poid> <poid>5006</poid> </root>
WITH tempid(id, elem) AS (SELECT custid, XMLELEMENT(NAME "poid", XMLNAMESPACES(DEFAULT 'http://mytest.uri'), poid) FROM purchaseorder ) SELECT XMLELEMENT(NAME "root", XMLNAMESPACES(DEFAULT 'http://mytest.uri'), XMLATTRIBUTES(cid), (SELECT XMLAGG(elem) FROM tempid WHERE tempid.id = customer.cid ) ) FROM customer WHERE cid = '1002'
WITH tempid(id, elem) AS (SELECT custid, XMLELEMENT(NAME "poid", poid) FROM purchaseorder ) SELECT XMLELEMENT(NAME "root", XMLNAMESPACES(DEFAULT 'http://mytest.uri'), XMLATTRIBUTES(cid), (SELECT XMLAGG(elem) FROM tempid WHERE tempid.id = customer.cid ) ) FROM customer WHERE cid = '1002'
xmlns="http://mytest.uri"
<root xmlns="http://mytest.uri" CID="1002"> <poid xmlns="">5000</poid> <poid xmlns="">5003</poid> <poid xmlns="">5006</poid> </root>