XMLELEMENT is an SQL/XML constructor that produces a well-formed XML element. You supply the element name, optional XMLATTRIBUTES(), and one or more content expressions. The function returns a value of the XML data type (or VARCHAR/NCLOB in some systems) that can be further queried or stored. Because it is evaluated per row, each invocation can generate different XML based on column values. XMLELEMENT is part of the SQL:2003 SQL/XML standard and is supported by engines that implement native XML types (PostgreSQL, Oracle, DB2, etc.). The element name must be a valid XML local name. Content expressions are concatenated in the order provided and automatically escaped. Null content is skipped unless the NULL ON NULL clause is used (PostgreSQL).
element_name STRING
- Tag name of the element.XMLATTRIBUTES clause
- Optional list that maps SQL expressions to attribute names.content_expr ANY
- One or more expressions that become the element’s children (text nodes or nested XML).XMLATTRIBUTES, XMLAGG, XMLFOREST, XMLSERIALIZE, JSON_OBJECT, FOR XML
SQL:2003 (SQL/XML)
It returns a value of the XML data type containing the constructed element. In engines without XML types it may return CLOB/VARCHAR.
Add an XMLATTRIBUTES clause: XMLATTRIBUTES(expr AS attr_name). Multiple attributes are comma-separated.
Yes. You can place XMLELEMENT inside another XMLELEMENT to build complex, hierarchical XML documents.
Content expressions that evaluate to NULL are ignored. To force explicit nil attributes, use database-specific options like NULL ON NULL in PostgreSQL.