XMLATTRIBUTES is an SQL/XML constructor used inside XMLELEMENT to generate one or more attributes for the resulting XML element. Each attribute is defined by an expression followed by the keyword AS and the desired attribute name. When the query runs, the database evaluates every expression, converts the result to a string according to XML rules, and inserts it as an attribute on the parent element. NULL expressions are omitted, preventing empty attributes. Attribute names can be quoted or unquoted identifiers; they are case sensitive in most engines. The feature is part of the ISO SQL:2003 SQL/XML specification and is natively supported in Oracle, PostgreSQL, IBM Db2, and other standards-compliant systems.
expressionN
(any SQL expression) - value to be stored as the attribute’s textattribute_nameN
(identifier or quoted identifier) - name of the resulting XML attributeXMLELEMENT, XMLFOREST, XMLAGG, XMLPARSE, XMLSERIALIZE, FOR XML (SQL Server)
SQL:2003 (SQL/XML)
Oracle, PostgreSQL, IBM Db2, and Firebird support XMLATTRIBUTES. MySQL, SQL Server, and SQLite do not implement it natively.
No. When an expression returns NULL, the attribute is completely omitted, ensuring clean XML output.
Attribute names must be literals in the query. Only the attribute values can be supplied from expressions.
XMLATTRIBUTES generates attributes for a single element, while XMLFOREST creates multiple child elements. They are complementary tools in SQL/XML.