XMLGEN function [String]

Generates an XML value based on an XQuery constructor.

Syntax
XMLGEN( xquery-constructor, content-expression [ AS variable-name ],... )
Parameters
  • xquery-constructor   An XQuery constructor. The XQuery constructor is an item defined in the XQuery language. It gives a syntax for constructing XML elements based on XQuery expressions. The xquery-constructor argument must be a well-formed XML document with one or more variable references. A variable reference is enclosed in curly braces and must be prefixed with a $ and have no surrounding white space. For example:
    SELECT XMLGEN( '<a>{$x}</a>', 1 AS x );

  • content-expression   A variable. You can specify multiple content-expression arguments. The optional variable-name argument is used to name the variable. For example,
    SELECT XMLGEN( '<emp EmployeeID="{$EmployeeID}"><StartDate>{$x}</StartDate></emp>',
                   EmployeeID, StartDate 
                   AS x )
    FROM Employees;

Remarks

Computed constructors as defined in the XQuery specification are not supported by the XMLGEN function.

When you execute a query that contains an XMLGEN function, data in BINARY, LONG BINARY, IMAGE, and VARBINARY columns is automatically returned in base64-encoded format.

Element content is always escaped unless the data type is XML. Illegal XML element and attribute names are also escaped.

For information about escaping and the XMLGEN function, see Invalid names and SQL/XML.

See also
Standards and compatibility
  • Part of the SQL/XML draft standard.
Example

The following example generates an <emp> element, as well as <Surname>, <GivenName>, and <StartDate> elements for each employee.

SELECT XMLGEN( '<emp EmployeeID="{$EmployeeID}">
                  <Surname>="{$Surname}"</Surname>
                  <GivenName>="{$GivenName}"</GivenName>
                  <StartDate>="{$StartDate}"</StartDate>
               </emp>',
               EmployeeID,
               Surname,
               GivenName,
               StartDate
              ) AS employee_list
FROM Employees;