Home Explore Blog CI



postgresql

6th chunk of `doc/src/sgml/xml2.sgml`
dd3c05df37d056bd7a2de22d5288ef2ab3ac3a5d322d740f0000000100000ced
 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, '&lt;doc num="C1"&gt;
&lt;line num="L1"&gt;&lt;a&gt;1&lt;/a&gt;&lt;b&gt;2&lt;/b&gt;&lt;c&gt;3&lt;/c&gt;&lt;/line&gt;
&lt;line num="L2"&gt;&lt;a&gt;11&lt;/a&gt;&lt;b&gt;22&lt;/b&gt;&lt;c&gt;33&lt;/c&gt;&lt;/line&gt;
&lt;/doc&gt;');

INSERT INTO test VALUES (2, '&lt;doc num="C2"&gt;
&lt;line num="L1"&gt;&lt;a&gt;111&lt;/a&gt;&lt;b&gt;222&lt;/b&gt;&lt;c&gt;333&lt;/c&gt;&lt;/line&gt;
&lt;line num="L2"&gt;&lt;a&gt;111&lt;/a&gt;&lt;b&gt;222&lt;/b&gt;&lt;c&gt;333&lt;/c&gt;&lt;/line&gt;
&lt;/doc&gt;');

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>

Title: XSLT Functions and Module Information
Summary
The section describes the availability of XSLT functions when libxslt is installed, including the xslt_process function for applying XSL stylesheets to documents, and provides information about the module's author and development sponsor.