postgresql_16:3.6.15.4 Mapping Tables to XML
PostgreSQL 16 > The SQL Language > Functions and Operators > XML Functions > Mapping Tables to XML
The following functions map the contents of relational tables to XML values. They can be thought of as XML export functionality: table_to_xml ( table regclass, nulls boolean, tableforest boolean, targetns text ) xml query_to_xml ( query text, nulls boolean, tableforest boolean, targetns text ) xml cursor_to_xml ( cursor refcursor, count integer, nulls boolean, tableforest boolean, targetns text ) xml
table_to_xml maps the content of the named table, passed as parameter table. The regclass type accepts strings identifying tables using the usual notation, including optional schema qualification and double quotes (see (see postgresql_16:3.5.19) for details). query_to_xml executes the query whose text is passed as parameter query and maps the result set. cursor_to_xml fetches the indicated number of rows from the cursor specified by the parameter cursor. This variant is recommended if large tables have to be mapped, because the result value is built up in memory by each function.
If tableforest is false, then the resulting XML document looks like this: <tablename> <row> <columnname1>data</columnname1> <columnname2>data</columnname2> </row> <row> ... </row> ... </tablename> If tableforest is true, the result is an XML content fragment that looks like this: <tablename> <columnname1>data</columnname1> <columnname2>data</columnname2> </tablename> <tablename> ... </tablename> ... If no table name is available, that is, when mapping a query or a cursor, the string table is used in the first format, row in the second format.
The choice between these formats is up to the user. The first format is a proper XML document, which will be important in many applications. The second format tends to be more useful in the cursor_to_xml function if the result values are to be reassembled into one document later on. The functions for producing XML content discussed above, in particular xmlelement, can be used to alter the results to taste.
The data values are mapped in the same way as described for the function xmlelement above.
The parameter nulls determines whether null values should be included in the output. If true, null values in columns are represented as: <columnname xsi:nil="true"/> where xsi is the XML namespace prefix for XML Schema Instance. An appropriate namespace declaration will be added to the result value. If false, columns containing null values are simply omitted from the output.
The parameter targetns specifies the desired XML namespace of the result. If no particular namespace is wanted, an empty string should be passed.
The following functions return XML Schema documents describing the mappings performed by the corresponding functions above: table_to_xmlschema ( table regclass, nulls boolean, tableforest boolean, targetns text ) xml query_to_xmlschema ( query text, nulls boolean, tableforest boolean, targetns text ) xml cursor_to_xmlschema ( cursor refcursor, nulls boolean, tableforest boolean, targetns text ) xml It is essential that the same parameters are passed in order to obtain matching XML data mappings and XML Schema documents.
The following functions produce XML data mappings and the corresponding XML Schema in one document (or forest), linked together. They can be useful where self-contained and self-describing results are wanted: table_to_xml_and_xmlschema ( table regclass, nulls boolean, tableforest boolean, targetns text ) xml query_to_xml_and_xmlschema ( query text, nulls boolean, tableforest boolean, targetns text ) xml
In addition, the following functions are available to produce analogous mappings of entire schemas or the entire current database: schema_to_xml ( schema name, nulls boolean, tableforest boolean, targetns text ) xml schema_to_xmlschema ( schema name, nulls boolean, tableforest boolean, targetns text ) xml schema_to_xml_and_xmlschema ( schema name, nulls boolean, tableforest boolean, targetns text ) xml database_to_xml ( nulls boolean, tableforest boolean, targetns text ) xml database_to_xmlschema ( nulls boolean, tableforest boolean, targetns text ) xml database_to_xml_and_xmlschema ( nulls boolean, tableforest boolean, targetns text ) xml These functions ignore tables that are not readable by the current user. The database-wide functions additionally ignore schemas that the current user does not have USAGE (lookup) privilege for.
Note that these potentially produce a lot of data, which needs to be built up in memory. When requesting content mappings of large schemas or databases, it might be worthwhile to consider mapping the tables separately instead, possibly even through a cursor.
The result of a schema content mapping looks like this: <schemaname> table1-mapping table2-mapping ... </schemaname> where the format of a table mapping depends on the tableforest parameter as explained above.
The result of a database content mapping looks like this: <dbname> <schema1name> ... </schema1name> <schema2name> ... </schema2name> ... </dbname> where the schema mapping is as above.
As an example of using the output produced by these functions, (see xslt-xml-html) shows an XSLT stylesheet that converts the output of table_to_xml_and_xmlschema to an HTML document containing a tabular rendition of the table data. In a similar manner, the results from these functions can be converted into other XML-based formats.
XSLT Stylesheet for Converting SQL/XML Output to HTML
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns="http://www.w3.org/1999/xhtml"
>
<xsl:output method="xml"
doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"
doctype-public="-//W3C/DTD XHTML 1.0 Strict//EN"
indent="yes"/>
<xsl:template match="/*">
<xsl:variable name="schema" select="//xsd:schema"/>
<xsl:variable name="tabletypename"
select="$schema/xsd:element[@name=name(current())]/@type"/>
<xsl:variable name="rowtypename"
select="$schema/xsd:complexType[@name=$tabletypename]/xsd:sequence/xsd:element[@name='row']/@type"/>
<html>
<head>
<title><xsl:value-of select="name(current())"/></title>
</head>
<body>
<table>
<tr>
<xsl:for-each select="$schema/xsd:complexType[@name=$rowtypename]/xsd:sequence/xsd:element/@name">
<th><xsl:value-of select="."/></th>
</xsl:for-each>
</tr>
<xsl:for-each select="row">
<tr>
<xsl:for-each select="*">
<td><xsl:value-of select="."/></td>
</xsl:for-each>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>