1- <!-- $PostgreSQL: pgsql/doc/src/sgml/func.sgml,v 1.360 2007/02/1603:50:29 momjian Exp $ -->
1+ <!-- $PostgreSQL: pgsql/doc/src/sgml/func.sgml,v 1.361 2007/02/1607:46:54 petere Exp $ -->
22
33 <chapter id="functions">
44 <title>Functions and Operators</title>
@@ -11156,6 +11156,193 @@ SELECT xmlroot(xmlparse(document '<?xml version="1.1"?><content>abc</content>'),
1115611156 </sect3>
1115711157 </sect2>
1115811158
11159+ <sect2>
11160+ <title>Mapping Tables to XML</title>
11161+
11162+ <para>
11163+ The following functions map the contents of relational tables to
11164+ XML values. They can be thought of as XML export functionality.
11165+ <synopsis>
11166+ table_to_xml(tbl regclass, nulls boolean, tableforest boolean, targetns text)
11167+ query_to_xml(query text, nulls boolean, tableforest boolean, targetns text)
11168+ cursor_to_xml(cursor refcursor, count int, nulls boolean, tableforest boolean, targetns text)
11169+ </synopsis>
11170+ The return type of each function is <type>xml</type>.
11171+ </para>
11172+
11173+ <para>
11174+ <function>table_to_xml</function> maps the content of the named
11175+ table, passed as parameter <parameter>tbl</parameter>. The
11176+ <type>regclass</type> accepts strings identifying tables using the
11177+ usual notation, including optional schema qualifications and
11178+ double quotes. <function>query_to_xml</function> executes the
11179+ query whose text is passed as parameter
11180+ <parameter>query</parameter> and maps the result set.
11181+ <function>cursor_to_xml</function> fetches the indicated number of
11182+ rows from the cursor specificed by the parameter
11183+ <parameter>cursor</parameter>. This variant is recommendable if
11184+ large tables have to be mapped, because the result value is built
11185+ up in memory by each function.
11186+ </para>
11187+
11188+ <para>
11189+ If <parameter>tableforest</parameter> is false, then the resulting
11190+ XML document looks like this:
11191+ <screen><![CDATA[
11192+ <tablename>
11193+ <row>
11194+ <columnname1>data</columnname1>
11195+ <columnname2>data</columnname2>
11196+ </row>
11197+
11198+ <row>
11199+ ...
11200+ </row>
11201+
11202+ ...
11203+ </tablename>
11204+ ]]></screen>
11205+
11206+ If <parameter>tableforest</parameter> is true, the result is an
11207+ XML content fragment that looks like this:
11208+ <screen><![CDATA[
11209+ <tablename>
11210+ <columnname1>data</columnname1>
11211+ <columnname2>data</columnname2>
11212+ </tablename>
11213+
11214+ <tablename>
11215+ ...
11216+ </tablename>
11217+
11218+ ...
11219+ ]]></screen>
11220+
11221+ If no table name is avaible, that is, when mapping a query or a
11222+ cursor, the string <literal>table</literal> is used in the first
11223+ format, <literal>row</literal> in the second format.
11224+ </para>
11225+
11226+ <para>
11227+ The choice between these formats is up to the user. The first
11228+ format is a proper XML document, which will be important in many
11229+ applications. The second format tends to be more useful in the
11230+ <function>cursor_to_xml</function> function if the result values are to be
11231+ reassembled into one document later on. The functions for
11232+ producing XML content discussed above, in particular
11233+ <function>xmlelement</function>, can be used to alter the results
11234+ to taste.
11235+ </para>
11236+
11237+ <para>
11238+ The data values are mapping in the same way as described for the
11239+ function <function>xmlelement</function> above.
11240+ </para>
11241+
11242+ <para>
11243+ The parameter <parameter>nulls</parameter> determines whether null
11244+ values should be included in the output. If true, null values in
11245+ columns are represented as
11246+ <screen><![CDATA[
11247+ <columname xsi:nil="true"/>
11248+ ]]></screen>
11249+ where <literal>xsi</literal> is the XML namespace prefix for XML
11250+ Schema Instance. An appropriate namespace declaration will be
11251+ added to the result value. If false, columns containing null
11252+ values are simply omitted from the output.
11253+ </para>
11254+
11255+ <para>
11256+ The parameter <parameter>targetns</parameter> specifies the
11257+ desired XML namespace of the result. If no particular namespace
11258+ is wanted, an empty string should be passed.
11259+ </para>
11260+
11261+ <para>
11262+ The following functions return XML Schema documents describing the
11263+ mappings made by the data mappings produced by the corresponding
11264+ functions above.
11265+ <synopsis>
11266+ table_to_xmlschema(tbl regclass, nulls boolean, tableforest boolean, targetns text)
11267+ query_to_xmlschema(query text, nulls boolean, tableforest boolean, targetns text)
11268+ cursor_to_xmlschema(cursor refcursor, nulls boolean, tableforest boolean, targetns text)
11269+ </synopsis>
11270+ It is essential that the same parameters are passed in order to
11271+ obtain matching XML data mappings and XML Schema documents.
11272+ </para>
11273+
11274+ <para>
11275+ The following functions produce XML data mappings and the
11276+ corresponding XML Schema in one document (or forest), linked
11277+ together. They can be useful where self-contained and
11278+ self-describing results are wanted.
11279+ <synopsis>
11280+ table_to_xml_and_xmlschema(tbl regclass, nulls boolean, tableforest boolean, targetns text)
11281+ query_to_xml_and_xmlschema(query text, nulls boolean, tableforest boolean, targetns text)
11282+ </synopsis>
11283+ </para>
11284+
11285+ <para>
11286+ As an example for using the output produced by these functions,
11287+ <xref linkend="xslt-xml-html"> shows an XSLT stylesheet that
11288+ converts the output of
11289+ <function>table_to_xml_and_xmlschema</function> to an HTML
11290+ document containing a tabular rendition of the table data. In a
11291+ similar manner, the result data of these functions can be
11292+ converted into other XML-based formats.
11293+ </para>
11294+
11295+ <figure id="xslt-xml-html">
11296+ <title>XSLT stylesheet for converting SQL/XML output to HTML</title>
11297+ <programlisting><![CDATA[
11298+ <?xml version="1.0"?>
11299+ <xsl:stylesheet version="1.0"
11300+ xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
11301+ xmlns:xsd="http://www.w3.org/2001/XMLSchema"
11302+ xmlns="http://www.w3.org/1999/xhtml"
11303+ >
11304+
11305+ <xsl:output method="xml"
11306+ doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"
11307+ doctype-public="-//W3C/DTD XHTML 1.0 Strict//EN"
11308+ indent="yes"/>
11309+
11310+ <xsl:template match="/*">
11311+ <xsl:variable name="schema" select="//xsd:schema"/>
11312+ <xsl:variable name="tabletypename"
11313+ select="$schema/xsd:element[@name=name(current())]/@type"/>
11314+ <xsl:variable name="rowtypename"
11315+ select="$schema/xsd:complexType[@name=$tabletypename]/xsd:sequence/xsd:element[@name='row']/@type"/>
11316+
11317+ <html>
11318+ <head>
11319+ <title><xsl:value-of select="name(current())"/></title>
11320+ </head>
11321+ <body>
11322+ <table>
11323+ <tr>
11324+ <xsl:for-each select="$schema/xsd:complexType[@name=$rowtypename]/xsd:sequence/xsd:element/@name">
11325+ <th><xsl:value-of select="."/></th>
11326+ </xsl:for-each>
11327+ </tr>
11328+
11329+ <xsl:for-each select="row">
11330+ <tr>
11331+ <xsl:for-each select="*">
11332+ <td><xsl:value-of select="."/></td>
11333+ </xsl:for-each>
11334+ </tr>
11335+ </xsl:for-each>
11336+ </table>
11337+ </body>
11338+ </html>
11339+ </xsl:template>
11340+
11341+ </xsl:stylesheet>
11342+ ]]></programlisting>
11343+ </figure>
11344+ </sect2>
11345+
1115911346 <sect2>
1116011347 <title>Processing XML</title>
1116111348
@@ -11171,21 +11358,6 @@ SELECT xmlroot(xmlparse(document '<?xml version="1.1"?><content>abc</content>'),
1117111358 </para>
1117211359
1117311360 <variablelist>
11174- <varlistentry>
11175- <term>Import/Export</term>
11176- <listitem>
11177-
11178- <para>
11179- There is no facility for mapping <acronym>XML</> to relational
11180- tables. An external tool must be used for this. One simple way
11181- to export <acronym>XML</> is to use <application>psql</> in
11182- <acronym>HTML</> mode (<literal>\pset format html</>), and
11183- convert the <acronym>XHTML</> output to XML using an external
11184- tool.
11185- </para>
11186- </listitem>
11187- </varlistentry>
11188-
1118911361 <varlistentry>
1119011362 <term>Indexing</term>
1119111363 <listitem>