diff --git a/jOOQ/src/main/java/org/jooq/XMLQueryPassingStep.java b/jOOQ/src/main/java/org/jooq/XMLQueryPassingStep.java
new file mode 100644
index 0000000000..9ab7ffc59c
--- /dev/null
+++ b/jOOQ/src/main/java/org/jooq/XMLQueryPassingStep.java
@@ -0,0 +1,78 @@
+/*
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * Other licenses:
+ * -----------------------------------------------------------------------------
+ * Commercial licenses for this work are available. These replace the above
+ * ASL 2.0 and offer limited warranties, support, maintenance, and commercial
+ * database integrations.
+ *
+ * For more information, please visit: http://www.jooq.org/licenses
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ */
+package org.jooq;
+
+// ...
+import static org.jooq.SQLDialect.POSTGRES;
+
+/**
+ * A step in the construction of an XMLQUERY expression.
+ *
+ * @author Lukas Eder
+ */
+public interface XMLQueryPassingStep {
+
+ /**
+ * Add the PASSING clause to the XMLQUERY
+ * expression.
+ */
+ @Support({ POSTGRES })
+ Field passing(XML xml);
+
+ /**
+ * Add the PASSING clause to the XMLQUERY
+ * expression.
+ */
+ @Support({ POSTGRES })
+ Field passing(Field xml);
+
+ /**
+ * Add the PASSING BY REF clause to the XMLQUERY
+ * expression.
+ */
+ @Support({ POSTGRES })
+ Field passingByRef(XML xml);
+
+ /**
+ * Add the PASSING BY REF clause to the XMLQUERY
+ * expression.
+ */
+ @Support({ POSTGRES })
+ Field passingByRef(Field xml);
+
+}
diff --git a/jOOQ/src/main/java/org/jooq/impl/XMLDocument.java b/jOOQ/src/main/java/org/jooq/impl/XMLDocument.java
new file mode 100644
index 0000000000..c7f70190f7
--- /dev/null
+++ b/jOOQ/src/main/java/org/jooq/impl/XMLDocument.java
@@ -0,0 +1,78 @@
+/*
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * Other licenses:
+ * -----------------------------------------------------------------------------
+ * Commercial licenses for this work are available. These replace the above
+ * ASL 2.0 and offer limited warranties, support, maintenance, and commercial
+ * database integrations.
+ *
+ * For more information, please visit: http://www.jooq.org/licenses
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ */
+package org.jooq.impl;
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/jOOQ/src/main/java/org/jooq/impl/XMLPassingMechanism.java b/jOOQ/src/main/java/org/jooq/impl/XMLPassingMechanism.java
new file mode 100644
index 0000000000..cd02afaf9b
--- /dev/null
+++ b/jOOQ/src/main/java/org/jooq/impl/XMLPassingMechanism.java
@@ -0,0 +1,40 @@
+/*
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * Other licenses:
+ * -----------------------------------------------------------------------------
+ * Commercial licenses for this work are available. These replace the above
+ * ASL 2.0 and offer limited warranties, support, maintenance, and commercial
+ * database integrations.
+ *
+ * For more information, please visit: http://www.jooq.org/licenses
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ */
+package org.jooq.impl;
+
+enum XMLPassingMechanism { BY_REF, BY_VALUE }
\ No newline at end of file
diff --git a/jOOQ/src/main/java/org/jooq/impl/XMLQuery.java b/jOOQ/src/main/java/org/jooq/impl/XMLQuery.java
new file mode 100644
index 0000000000..414527d647
--- /dev/null
+++ b/jOOQ/src/main/java/org/jooq/impl/XMLQuery.java
@@ -0,0 +1,158 @@
+/*
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * Other licenses:
+ * -----------------------------------------------------------------------------
+ * Commercial licenses for this work are available. These replace the above
+ * ASL 2.0 and offer limited warranties, support, maintenance, and commercial
+ * database integrations.
+ *
+ * For more information, please visit: http://www.jooq.org/licenses
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ */
+package org.jooq.impl;
+
+// ...
+import static org.jooq.conf.ParamType.INLINED;
+import static org.jooq.impl.DSL.select;
+import static org.jooq.impl.DSL.unnest;
+import static org.jooq.impl.DSL.xmlagg;
+import static org.jooq.impl.Keywords.K_BY;
+import static org.jooq.impl.Keywords.K_PASSING;
+import static org.jooq.impl.Keywords.K_REF;
+import static org.jooq.impl.Names.N_XMLQUERY;
+import static org.jooq.impl.SQLDataType.XML;
+import static org.jooq.impl.XMLPassingMechanism.BY_REF;
+
+import org.jooq.Context;
+import org.jooq.Field;
+import org.jooq.XML;
+import org.jooq.XMLQueryPassingStep;
+import org.jooq.conf.ParamType;
+
+/**
+ * @author Lukas Eder
+ */
+final class XMLQuery extends AbstractField implements XMLQueryPassingStep {
+
+ /**
+ * Generated UID
+ */
+ private static final long serialVersionUID = -4881363881968319258L;
+ private final Field xpath;
+ private final Field passing;
+ private final XMLPassingMechanism passingMechanism;
+
+ XMLQuery(Field xpath) {
+ this(xpath, null, null);
+ }
+
+ private XMLQuery(Field xpath, Field passing, XMLPassingMechanism passingMechanism) {
+ super(N_XMLQUERY, SQLDataType.XML);
+
+ this.xpath = xpath;
+ this.passing = passing;
+ this.passingMechanism = passingMechanism;
+ }
+
+ // -------------------------------------------------------------------------
+ // XXX: DSL API
+ // -------------------------------------------------------------------------
+ @Override
+ public final Field passing(XML xml) {
+ return passing(Tools.field(xml));
+ }
+
+ @Override
+ public final Field passing(Field xml) {
+ return new XMLQuery(xpath, xml, null);
+ }
+
+ @Override
+ public final Field passingByRef(XML xml) {
+ return passingByRef(Tools.field(xml));
+ }
+
+ @Override
+ public final Field passingByRef(Field xml) {
+ return new XMLQuery(xpath, xml, BY_REF);
+ }
+
+ // -------------------------------------------------------------------------
+ // XXX: QueryPart API
+ // -------------------------------------------------------------------------
+
+ @Override
+ public final void accept(Context> ctx) {
+ switch (ctx.family()) {
+ case POSTGRES:
+ Field x = DSL.field(DSL.name("x"), XML);
+
+ ctx.sql('(').visit(
+ select(xmlagg(x))
+ .from(unnest(DSL.field(
+ "xpath({0}, {1})", XML.getArrayDataType(), xpath, passing
+ )).as("t", x.getName())))
+ .sql(')');
+
+ break;
+
+ default:
+ ctx.visit(N_XMLQUERY).sql('(')
+ .formatIndentStart()
+ .formatNewLine();
+
+
+
+
+
+
+
+
+
+
+
+
+ ctx.visit(xpath);
+
+ ctx.formatSeparator()
+ .visit(K_PASSING);
+
+ if (passingMechanism == BY_REF)
+ ctx.sql(' ').visit(K_BY).sql(' ').visit(K_REF);
+
+ ctx.sql(' ').visit(passing)
+ .formatIndentEnd()
+ .formatNewLine()
+ .sql(')');
+
+ break;
+ }
+
+ }
+}