XMLElement() function

The XMLElement() function constructs an XML element. Typically, you nest the XMLElement() function to produce a hierarchically structured XML document.

Syntax

The XMLElement() function has the following syntax:

XML = XMLElement(varchar name, [XML_Attrib attrib,] varchar value);

The name value specifies the name of the enclosing tag for the XML element. If the identifier that you specify is NULL, no element is returned. The name cannot be a column name or column reference, a difference from the SQL/XML specification.

One or more optional attrib values specify one or more name-value pairs that create attributes for the XML element.

The input value specifies the content of the new XML element. This element can be either a scalar value or a nested XMLElement() function call.

Returns

The function returns type XML. It returns a compiled representation of an XML element with the specified name, content, and optionally a collection of attributes. It does not create prolog information.

Example

Consider the following example:
select XMLElement('Parent', XMLElement('Child', 'Child text'));
This example creates an XML value with the following content:
<Parent><Child>Child text</Child></Parent>
You can use a query such as the following one to display the XML content:
select XMLserialize(XMLElement('Parent', XMLElement('Child', 'Child text')));
                XMLSERIALIZE
--------------------------------------------
 <Parent><Child>Child text</Child></Parent>
(1 row)