Available in versions: Dev (3.21) | Latest (3.20) | 3.19 | 3.18 | 3.17 | 3.16 | 3.15 | 3.14

This documentation is for the unreleased development version of jOOQ. Click on the above version links to get this documentation for a supported version of jOOQ.

PATH mode

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

The PATH mode generates XML content based on the "path" as specified by column aliases.

Consider the following query

SELECT 
  id AS [book/id], 
  title AS [book/title]
FROM book
ORDER BY id
FOR XML PATH
 
create.select(
         BOOK.ID.as("book/id"), 
         BOOK.TITLE.as("book/title"))
      .from(BOOK)
      .orderBy(BOOK.ID)
      .forXML().path()
      .fetch();

This query produces a document fragment like this:

<row><book><ID>1</ID><TITLE>1984</TITLE></book></row>
<row><book><ID>2</ID><TITLE>Animal Farm</TITLE></book></row>
<row><book><ID>3</ID><TITLE>O Alquimista</TITLE></book></row>
<row><book><ID>4</ID><TITLE>Brida</TITLE></book></row>

Alternatively, provide an explicit element name for rows (the default being row, as above):

SELECT id, title
FROM book
ORDER BY id
FOR XML PATH ('book')
 
create.select(BOOK.ID, BOOK.TITLE)
      .from(BOOK)
      .orderBy(BOOK.ID)
      .forXML().path("book")
      .fetch();

This will produce

<book><ID>1</ID><TITLE>1984</TITLE></book>
<book><ID>2</ID><TITLE>Animal Farm</TITLE></book>
<book><ID>3</ID><TITLE>O Alquimista</TITLE></book>
<book><ID>4</ID><TITLE>Brida</TITLE></book>

Dialect support

This example using jOOQ:

select(BOOK.ID.as(quotedName("book/id"))).from(BOOK).orderBy(BOOK.ID).forXML().path()

Translates to the following dialect specific expressions:

DB2, Oracle, Postgres

SELECT xmlagg(xmlelement(
  NAME row,
  xmlelement(
    NAME book,
    xmlelement(NAME id, "book/id")
  )
))
FROM (
  SELECT BOOK.ID "book/id"
  FROM BOOK
  ORDER BY BOOK.ID
) t

SQLServer

SELECT (
  SELECT BOOK.ID [book/id]
  FROM BOOK
  ORDER BY BOOK.ID
  FOR XML PATH
)

Teradata

SELECT xmlagg(xmlelement(
  NAME row,
  xmlelement(
    NAME book,
    xmlelement(NAME id, "book/id")
  )
))
FROM (
  SELECT *
  FROM (
    SELECT TOP 999999999999999999 BOOK.ID "book/id"
    FROM BOOK
    ORDER BY BOOK.ID
  ) x
) t

ASE, Access, Aurora MySQL, Aurora Postgres, BigQuery, ClickHouse, CockroachDB, Databricks, Derby, DuckDB, Exasol, Firebird, H2, HSQLDB, Hana, Informix, MariaDB, MemSQL, MySQL, Redshift, SQLDataWarehouse, SQLite, Snowflake, Sybase, Trino, Vertica, YugabyteDB

/* UNSUPPORTED */
Generated with jOOQ 3.21. Support in older jOOQ versions may differ. Translate your own SQL on our website

References to this page

Feedback

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

The jOOQ Logo