result will appear only on the first row of the result. The solution
to this is to use the key field as part of a join against a simpler
XPath query. As an example:
<programlisting>
CREATE TABLE test (
id int PRIMARY KEY,
xml text
);
INSERT INTO test VALUES (1, '<doc num="C1">
<line num="L1"><a>1</a><b>2</b><c>3</c></line>
<line num="L2"><a>11</a><b>22</b><c>33</c></line>
</doc>');
INSERT INTO test VALUES (2, '<doc num="C2">
<line num="L1"><a>111</a><b>222</b><c>333</c></line>
<line num="L2"><a>111</a><b>222</b><c>333</c></line>
</doc>');
SELECT * FROM
xpath_table('id','xml','test',
'/doc/@num|/doc/line/@num|/doc/line/a|/doc/line/b|/doc/line/c',
'true')
AS t(id int, doc_num varchar(10), line_num varchar(10), val1 int, val2 int, val3 int)
WHERE id = 1 ORDER BY doc_num, line_num
id | doc_num | line_num | val1 | val2 | val3
----+---------+----------+------+------+------
1 | C1 | L1 | 1 | 2 | 3
1 | | L2 | 11 | 22 | 33
</programlisting>
</para>
<para>
To get <literal>doc_num</literal> on every line, the solution is to use two invocations
of <function>xpath_table</function> and join the results:
<programlisting>
SELECT t.*,i.doc_num FROM
xpath_table('id', 'xml', 'test',
'/doc/line/@num|/doc/line/a|/doc/line/b|/doc/line/c',
'true')
AS t(id int, line_num varchar(10), val1 int, val2 int, val3 int),
xpath_table('id', 'xml', 'test', '/doc/@num', 'true')
AS i(id int, doc_num varchar(10))
WHERE i.id=t.id AND i.id=1
ORDER BY doc_num, line_num;
id | line_num | val1 | val2 | val3 | doc_num
----+----------+------+------+------+---------
1 | L1 | 1 | 2 | 3 | C1
1 | L2 | 11 | 22 | 33 | C1
(2 rows)
</programlisting>
</para>
</sect3>
</sect2>
<sect2 id="xml2-xslt">
<title>XSLT Functions</title>
<para>
The following functions are available if libxslt is installed:
</para>
<sect3 id="xml2-xslt-xslt-process">
<title><literal>xslt_process</literal></title>
<indexterm>
<primary>xslt_process</primary>
</indexterm>
<synopsis>
xslt_process(text document, text stylesheet, text paramlist) returns text
</synopsis>
<para>
This function applies the XSL stylesheet to the document and returns
the transformed result. The <literal>paramlist</literal> is a list of parameter
assignments to be used in the transformation, specified in the form
<literal>a=1,b=2</literal>. Note that the
parameter parsing is very simple-minded: parameter values cannot
contain commas!
</para>
<para>
There is also a two-parameter version of <function>xslt_process</function> which
does not pass any parameters to the transformation.
</para>
</sect3>
</sect2>
<sect2 id="xml2-author">
<title>Author</title>
<para>
John Gray <email>jgray@azuli.co.uk</email>
</para>
<para>
Development of this module was sponsored by Torchbox Ltd. (www.torchbox.com).
It has the same BSD license as PostgreSQL.
</para>
</sect2>
</sect1>