XMLDOCUMENT scalar function
The XMLDOCUMENT function returns an XML value with a single document node and zero or more nodes as its children. The content of the generated XML document node is specified by a list of expressions.
The schema is SYSIBM.
- XML-expression
- An expression that returns an XML value. A sequence item in the XML value must not be an attribute node. If XML-expression returns a null value, it is ignored for further processing. However, if all XML-expression values are null, the result of the function is the null value.
The result of the function is an XML value.
The result can be null; if all of the arguments are null, the result is the null value.
The resulting XML value is built from the list of XML-expression arguments. The children of the resulting document node are constructed as follows:
- All of the non-null XML values that are returned by XML-expression are concatenated together. The result is a sequence of nodes or atomic values, which is referred to in the following steps as the input sequence. Any document node in the input sequence is replaced by copies of its children.
- For each node in the input sequence, a new deep copy of the node is constructed. A deep copy of a node is a copy of the whole subtree that is rooted at that node, including the node itself and its descendants and attributes. Each copied node has a new node identity. Copied element nodes are given the type annotation 'xdt:untyped', and copied attribute nodes are given the type annotation 'xdt:untypedAtomic'. For each adjacent sequence of one or more atomic values that is returned in the input sequence, a new text node is constructed that contains the result of casting each atomic value to a string, with a single blank character inserted between adjacent values. The resulting sequence of nodes is called the content sequence. Adjacent text nodes in the content sequence are merged into a single text node by concatenating the contents of the text nodes with no intervening blanks. After concatenation, any text node that contains a zero-length string is deleted from the content sequence.
- The nodes in the content sequence become the children of the new document node.
Example 1: Insert a constructed document into an XML
column:
INSERT INTO T1 VALUES(123,
(SELECT XMLDOCUMENT(XMLELEMENT(NAME "Emp",
e.fname || ' ' || e.lname),
XMLCOMMENT('This is just a simple example'))
FROM EMPLOYEE e
WHERE e.empid = 123));