New versions: Dev (3.15) | Latest (3.14)

XMLAGG

Applies to ✅ Open Source Edition   ✅ Express Edition   ✅ Professional Edition   ✅ Enterprise Edition

A data set can be aggregated into a org.jooq.XML element using XMLAGG

SELECT xmlelement(
  NAME ids,
  xmlagg(xmlelement(NAME id, id))
)
FROM author
create.select(xmlelement("ids",
        xmlagg(xmlelement("id", AUTHOR.ID))
      ))
      .from(AUTHOR)
      .fetch();

The result would look like this:

+---------------------------------+
| xmlelement                      |
+---------------------------------+
| <ids><id>1</id><id>2</id></ids> |
+---------------------------------+

Ordering aggregation contents

When aggregating data into XML, ordering may be relevant. For this, use the ORDER BY clause in XMLAGG

SELECT xmlelement(
  NAME ids,
  xmlagg(xmlelement(NAME id, id) ORDER BY id DESC)
)
FROM author
create.select(xmlelement("ids",
        xmlagg(xmlelement("id", AUTHOR.ID))
          .orderBy(AUTHOR.ID.desc())))
      .from(AUTHOR)
      .fetch();

The result would look like this:

+---------------------------------+
| xmlelement                      |
+---------------------------------+
| <ids><id>2</id><id>1</id></ids> |
+---------------------------------+

Dialect support

This example using jOOQ:

xmlagg(xmlelement("id", AUTHOR.ID))

Translates to the following dialect specific expressions:

-- DB2, ORACLE, POSTGRES
xmlagg(xmlelement(NAME id, AUTHOR.ID))

-- ACCESS, ASE, AURORA_MYSQL, AURORA_POSTGRES, COCKROACHDB, DERBY, FIREBIRD, H2, HANA, HSQLDB, INFORMIX, INGRES, MARIADB, 
-- MEMSQL, MYSQL, REDSHIFT, SQLDATAWAREHOUSE, SQLITE, SQLSERVER, SYBASE, TERADATA, VERTICA
/* UNSUPPORTED */

(These are currently generated with jOOQ 3.15, see #10141)

Feedback

Do you have any feedback about this page? We'd love to hear it!

The jOOQ Logo