XMLATTRIBUTES scalar function

The XMLATTRIBUTES function constructs XML attributes from the arguments.

Read syntax diagramSkip visual syntax diagramXMLATTRIBUTES( ,attribute-value-expressionASattribute-name )

The schema is SYSIBM. The function name cannot be specified as a qualified name.

This function can only be used as an argument of the XMLELEMENT function. The result is an XML sequence containing an XQuery attribute node for each non-null input value.

attribute-value-expression
An expression whose result is the attribute value. The data type of attribute-value-expression cannot be an XML type, a BINARY type, a VARBINARY type, or a structured type (SQLSTATE 42884). The expression can be any SQL expression. If the expression is not a simple column reference, an attribute name must be specified.
attribute-name
Specifies an attribute name. The name is an SQL identifier that must be in the form of an XML qualified name, or QName (SQLSTATE 42634). See the W3C XML namespace specifications for more details on valid names. The attribute name cannot be xmlns or prefixed with xmlns:. A namespace is declared using the function XMLNAMESPACES. Duplicate attribute names, whether implicit or explicit, are not allowed (SQLSTATE 42713).

If attribute-name is not specified, attribute-value-expression must be a column name (SQLSTATE 42703). The attribute name is created from the column name using the fully escaped mapping from a column name to an XML attribute name.

The data type of the result is XML. If the result of attribute-value-expression can be null, the result can be null; if the result of every attribute-value-expression is null, the result is the null value.

Examples

Note: XMLATTRIBUTES does not insert blank spaces or new line characters in the output. All example output has been formatted to enhance readability.
  • Example 1: Produce an element with attributes.
       SELECT E.EMPNO, XMLELEMENT(
         NAME "Emp",
         XMLATTRIBUTES(
           E.EMPNO, E.FIRSTNME ||' '|| E.LASTNAME AS "name"
         )
       )
       AS "Result"
       FROM EMPLOYEE E WHERE E.EDLEVEL = 12
    This query produces the following result:
    EMPNO  Result
    000290 <Emp EMPNO="000290" name="JOHN PARKER"></Emp>
    000310 <Emp EMPNO="000310" name="MAUDE SETRIGHT"></Emp>
    200310 <Emp EMPNO="200310" name="MICHELLE SPRINGER"></Emp>
  • Example 2: Produce an element with a namespace declaration that is not used in any QName. The prefix is used in an attribute value.
       VALUES XMLELEMENT(
         NAME "size",
         XMLNAMESPACES(
           'http://www.w3.org/2001/XMLSchema-instance' AS "xsi",
           'http://www.w3.org/2001/XMLSchema' AS "xsd"
         ),
         XMLATTRIBUTES(
           'xsd:string' AS "xsi:type"
         ), '1'
       )
    This query produces the following result:
    <size xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xmlns:xsd="http://www.w3.org/2001/XMLSchema"
      xsi:type="xsd:string">1</size>