You can construct XML values, which do not necessarily
have to be well-formed XML documents, by combining those publishing
functions that correspond to the components you want in the resulting
the XML value. The functions must be specified in the order that
you want the results to appear.
Values constructed using the SQL/XML publishing functions are returned
as XML. Depending on what you want to do with the XML value, you might
need to explicitly serialize the value to convert it to another SQL
data type. Refer to the documentation on XML serialization for details.
The following SQL/XML publishing functions can be used to construct
XML values:
- XMLAGG aggregate function
- Returns an XML sequence containing an item for each non-null value
in a set of XML values.
- XMLATTRIBUTES scalar function
- Constructs XML attributes from the arguments. This function can
be used only as an argument of the XMLELEMENT function.
- XMLCOMMENT scalar function
- Returns an XML value with a single XQuery comment node with the
input argument as the content.
- XMLCONCAT scalar function
- Returns a sequence containing the concatenation of a variable
number of XML input arguments.
- XMLDOCUMENT scalar function
- Returns an XML value with a single XQuery document node with zero
or more child nodes. This function creates a document node, which
by definition, every XML document must have. A document node is not
visible in the serialized representation of XML, however, every document
that is to be stored in a DB2® table
must contain a document node.
- XMLELEMENT scalar function
- Returns an XML value that is an XML element node. Note that the
XMLELEMENT function does not create a document node, only an element
node. When constructing XML documents that are to be inserted, it
is not sufficient to create only an element node. The document must
contain a document node, created with the XMLDOCUMENT function.
- XMLFOREST scalar function
- Returns an XML value that is a sequence of XML element nodes.
- XMLGROUP aggregate function
- Returns a single top-level element to represent a table or the
result of a query. By default each row in the result set is mapped
to a row subelement and each input expression is mapped to a subelement
of the row subelement. Optionally, each row in the result can be mapped
to a row subelement and each input expression to be mapped to an attribute
of the row subelement.
- XMLNAMESPACES declaration
- Constructs namespace declarations from the arguments. This declaration
can be used only as an argument of the XMLELEMENT, XMLFOREST, and
XMLTABLE functions.
- XMLPI scalar function
- Returns an XML value with a single XQuery processing instruction
node.
- XMLROW scalar function
- Returns a sequence of row elements to represent a table or the
result of a query. By default each input expression is transformed
into a subelement of a row element. Optionally, each input expression
can be transformed into an attribute of a row element.
- XMLTEXT scalar function
- Returns an XML value with a single XQuery text node having the
input argument as the content.
- XSLTRANSFORM scalar function
- Converts XML data into other formats, including other XML schemas.
Null element values
When an XML value is
constructed using XMLELEMENT or XMLFOREST, it is possible that a null
value is encountered when determining the element's content. The
EMPTY ON NULL and NULL ON NULL options of XMLELEMENT and XMLFOREST
allow you to specify whether an empty element or no element is generated
when an element's content is null. The default null handling for
XMLEXISTS is EMPTY ON NULL. The default null handling for XMLFOREST
is NULL ON NULL.