XMLDOCUMENT scalar function

The XMLDOCUMENT function returns an XML value with a single XQuery document node with zero or more children nodes.

Read syntax diagramSkip visual syntax diagramXMLDOCUMENT(XML-expression )

The schema is SYSIBM. The function name cannot be specified as a qualified name.

XML-expression
An expression that returns an XML value. A sequence item in the XML value must not be an attribute node (SQLSTATE 10507).

The data type of the result is XML. If the result of XML-expression can be null, the result can be null; if the input value is null, the result is the null value.

The children of the resulting document node are constructed as described in the following steps. The input expression is a sequence of nodes or atomic values, which is referred to in these steps as the content sequence.
  1. If the content sequence contains a document node, the document node is replaced in the content sequence by the children of the document node.
  2. Each adjacent sequence of one or more atomic values in the content sequence are replaced with a text node containing the result of casting each atomic value to a string with a single blank character inserted between adjacent values.
  3. For each node in the content sequence, a new deep copy of the node is constructed. A deep copy of a node is a copy of the whole subtree rooted at that node, including the node itself and its descendants. Each copied node has a new node identity.
  4. The nodes in the content sequence become the children of the new document node.
The XMLDOCUMENT function effectively executes the XQuery computed document constructor. The result of
XMLQUERY('document {$E}' PASSING BY REF XML-expression AS "E")
is equivalent to
XMLDOCUMENT( XML-expression )
with the exception of the case where XML-expression is null and XMLQUERY returns the empty sequence compared to XMLDOCUMENT which returns the null value.

Example

Insert a constructed document into an XML column.
   INSERT INTO T1 VALUES(
     123, (
       SELECT XMLDOCUMENT(
         XMLELEMENT(
           NAME "Emp", E.FIRSTNME || ' ' || E.LASTNAME, XMLCOMMENT(
             'This is just a simple example'
           )
         )
       )
       FROM EMPLOYEE E
       WHERE E.EMPNO = '000120'
     )
   )