1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
|
<?xml version="1.0" encoding="ISO-8859-1"?>
<!--
-
- This file is part of the OpenLink Software Virtuoso Open-Source (VOS)
- project.
-
- Copyright (C) 1998-2018 OpenLink Software
-
- This project is free software; you can redistribute it and/or modify it
- under the terms of the GNU General Public License as published by the
- Free Software Foundation; only version 2 of the License, dated June 1991.
-
- This program is distributed in the hope that it will be useful, but
- WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- General Public License for more details.
-
- You should have received a copy of the GNU General Public License along
- with this program; if not, write to the Free Software Foundation, Inc.,
- 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
-
-
-->
<refentry id="fn_XMLAGG">
<refmeta>
<refentrytitle>XMLAGG</refentrytitle>
<refmiscinfo>xml</refmiscinfo>
</refmeta>
<refnamediv>
<refname>XMLAGG</refname>
<refpurpose>Produces a forest of elements from a collection of XML values</refpurpose>
</refnamediv>
<refsynopsisdiv>
<funcsynopsis id="fsyn_XMLAGG">
<funcprototype id="fproto_XMLAGG">
<funcdef>vector <function>XMLAGG</function></funcdef>
<paramdef><parameter>value_expression</parameter> any</paramdef>
</funcprototype>
</funcsynopsis>
</refsynopsisdiv>
<refsect1 id="desc_XMLAGG"><title>Description</title>
<para><function>XMLAGG</function> is aggregate function that produces a forest of XML elements from the given list of xml elements.
It concatenates the values returned from one column of multiple rows, unlike <function>XMLCONCAT</function>,
which concatenates the values returned from multiple columns in the same row.</para>
<para>The order of element in the result of <function>XMLAGG</function> is defined by the order of retrieval of the
source data rows. It is important to remember that the order of rows in an SQL resultset defined only if there's an explicit
ORDER BY clause. Hence if the order of elements in the resulting forest is important then XMLAGG should be applied to data
that comes from inner SELECT statement that has an ORDER BY clause, not e.g. from a table reference.</para>
<para>Note that <function>XMLAGG</function> is actually declared as <function>DB.DBA.XMLAGG</function> but it is not
important for plain use: for compatibility with other systems, any call of <function>XMLAGG</function> in any SQl statement
is always replaced with the call of <function>DB.DBA.XMLAGG</function>, no matter which qualifier and user name are in use.</para>
</refsect1>
<refsect1 id="params_XMLAGG"><title>Parameters</title>
<refsect2><title>value_expression</title>
<para>the result of one of the following functions <function>XMLAGG</function>, <function>XMLCONCAT</function>,
<function>XMLELEMENT</function>, or <function>XMLFOREST</function>.</para>
</refsect2>
</refsect1>
<refsect1 id="ret_XMLAGG"><title>Return Types</title>
<para>The aggregate returns a vector that is a suitable input for functions <function>XMLELEMENT</function>,
<function>XMLCONCAT</function> and <function>xml_tree_doc</function>.</para>
</refsect1>
<refsect1 id="examples_XMLAGG"><title>Examples</title>
<example id="ex_XMLAGG_1"><title>XMLAGG() enclosed in XMLELEMENT()</title>
<para>The following example produces an 'Emp' element with attribute 'Title' and a list of all employees having
the title 'Sales Representative' as element content.</para>
<programlisting><![CDATA[
select XMLELEMENT ('Emp', XMLATTRIBUTES ('Sales Representative' as "Title"),
XMLAGG (XMLELEMENT ('Name', "FirstName", ' ', "LastName")))
from "Demo"."demo"."Employees"
where "Title" = 'Sales Representative';
callret
VARCHAR
_______________________________________________________________________________
<Emp Title="Sales Representative">
<Name>Nancy Davolio</Name>
<Name>Janet Leverling</Name>
<Name>Margaret Peacock</Name>
<Name>Michael Suyama</Name>
<Name>Robert King</Name>
<Name>Anne Dodsworth</Name>
</Emp>
1 Rows.
]]></programlisting>
</example>
<example id="ex_XMLAGG_2"><title>XMLAGG() that produces a sorted document</title>
<para>The result of the previous example contains an unsorted list of names. This is because data rows were retrieved
from "Demo"."demo"."Employees" in primary key order, and this order has nothing common with fields "FirstName"
and "LastName". To produce the sorted result, the query should contain ORDER BY in a subquery. These two variants
will work identically if "FirstName" never contains whitespaces or nonprintable control characters, but the last
one is formally more correct.</para>
<programlisting><![CDATA[
select XMLELEMENT ('Emp', XMLATTRIBUTES ('Sales Representative' as "Title"),
XMLAGG (XMLELEMENT ('Name', "FirstName", ' ', "LastName")))
from
(select "FirstName", "LastName"
from "Demo"."demo"."Employees"
where "Title"= 'Sales Representative'
order by 1, 2) as subq;
callret
VARCHAR
_______________________________________________________________________________
<Emp Title="Sales Representative">
<Name>Anne Dodsworth</Name>
<Name>Janet Leverling</Name>
<Name>Margaret Peacock</Name>
<Name>Michael Suyama</Name>
<Name>Nancy Davolio</Name>
<Name>Robert King</Name>
</Emp>
1 Rows.
select XMLELEMENT ('Emp', XMLATTRIBUTES ('Sales Representative' as "Title"),
XMLAGG (XMLELEMENT ('Name', full_name)))
from
(select concat ("FirstName", ' ', "LastName") as full_name
from "Demo"."demo"."Employees"
where "Title"= 'Sales Representative'
order by 1) as subq;
callret
VARCHAR
_______________________________________________________________________________
<Emp Title="Sales Representative">
<Name>Anne Dodsworth</Name>
<Name>Janet Leverling</Name>
<Name>Margaret Peacock</Name>
<Name>Michael Suyama</Name>
<Name>Nancy Davolio</Name>
<Name>Robert King</Name>
</Emp>
1 Rows.
]]></programlisting>
</example>
</refsect1>
<refsect1 id="seealso_XMLAGG"><title>See Also</title>
<para><link linkend="fn_XMLELEMENT">XMLELEMENT()</link></para>
<para><link linkend="fn_XMLATTRIBUTES">XMLATTRIBUTES()</link></para>
<para><link linkend="fn_XMLFOREST">XMLFOREST()</link></para>
<para><link linkend="fn_XMLCONCAT">XMLCONCAT()</link></para>
</refsect1>
</refentry>
|