XML functions

Devart ODBC Driver for PostgreSQL supports the following XML functions and expressions for generating, querying, and exporting XML data.
They cover XML construction, predicates, XPath-based processing, and mapping tables or queries to XML and XML Schema.

Producing XML content

Function / Expression Return type Description
xmltext(text) xml Wraps a text value in an XML text node and escapes XML special characters.
xmlcomment(text) xml Creates an XML comment node. The content must not contain -- or end with -.
xmlconcat(xml, …) xml Concatenates multiple XML fragments into a single XML value; NULL arguments are ignored.
xmlelement(NAME name, …, content …) xml Creates a new XML element with the specified name, optional attributes, and content.
xmlforest(content AS name, …) xml Builds a sequence (“forest”) of XML elements, each using its own name and content.
xmlpi(NAME name [, content]) xml Creates an XML processing instruction; the content must not contain ?>.
xmlroot(xml, VERSION {text | NO VALUE} [, STANDALONE {YES | NO | NO VALUE}]) xml Modifies the root node properties (version and standalone flag) of an existing XML value.
xmlagg(xml) (aggregate) xml Aggregates and concatenates XML values from multiple rows into a single XML fragment.

XML predicates

Expression / Function Return type Description
xml IS DOCUMENT boolean Returns true if the xml value is a well-formed XML document (single root element), not just a fragment.
xml IS NOT DOCUMENT boolean Returns true if the xml value is not a full document (it is an XML fragment).
XMLEXISTS(text PASSING [BY REF] xml [BY REF]) boolean Evaluates an XPath expression against an XML value and returns true if the expression yields at least one node.
xml_is_well_formed(text) boolean Returns true if the input text is well-formed XML. Interpretation (document vs content) depends on the xmloption setting.
xml_is_well_formed_document(text) boolean Returns true if the input text is a well-formed XML document.
xml_is_well_formed_content(text) boolean Returns true if the input text is well-formed XML content (not necessarily a full document).

Processing XML

Function / Expression Return type Description
xpath(xpath_expr text, xml [, nsarray text[]]) xml[] Evaluates an XPath 1.0 expression on an XML value and returns an array of matching XML nodes or values. Optional namespace mappings can be passed in nsarray.
xpath_exists(xpath_expr text, xml [, nsarray text[]]) boolean Returns true if the XPath expression matches at least one node in the XML value; similar to XMLEXISTS but supports explicit namespace mappings.
XMLTABLE([XMLNAMESPACES(...)], row_expression PASSING document_expression, COLUMNS …) setof record Evaluates an XPath expression and projects the result into a set of rows and columns, mapping XML nodes and attributes to SQL columns.

Mapping tables and queries to XML

Function Return type Description
table_to_xml(table regclass, nulls boolean, tableforest boolean, targetns text) xml Exports the contents of a table as XML using the specified options for NULL handling, table/row layout, and target namespace.
query_to_xml(query text, nulls boolean, tableforest boolean, targetns text) xml Runs an SQL query and maps its result set to XML with configurable NULL handling, layout, and target namespace.
cursor_to_xml(cursor refcursor, count integer, nulls boolean, tableforest boolean, targetns text) xml Reads a defined number of rows from a cursor and exports them as XML.
table_to_xmlschema(table regclass, nulls boolean, tableforest boolean, targetns text) xml Generates an XML Schema (XSD) that describes the XML representation produced by table_to_xml.
query_to_xmlschema(query text, nulls boolean, tableforest boolean, targetns text) xml Generates an XSD schema for the XML output of query_to_xml.
cursor_to_xmlschema(cursor refcursor, nulls boolean, tableforest boolean, targetns text) xml Generates an XSD schema for the XML output of cursor_to_xml.
table_to_xml_and_xmlschema(...) xml Produces a combined XML document containing both table data and its associated XML Schema.
query_to_xml_and_xmlschema(...) xml Produces a combined XML document containing query results and the corresponding XML Schema.
schema_to_xml(schema name, nulls boolean, tableforest boolean, targetns text) xml Exports the data from all tables in a schema to XML.
schema_to_xmlschema(...) xml Generates an XML Schema for the XML representation of a database schema.
schema_to_xml_and_xmlschema(...) xml Exports schema data and the corresponding XML Schema in a single XML document.
database_to_xml(nulls boolean, tableforest boolean, targetns text) xml Exports the entire database contents to XML.
database_to_xmlschema(nulls boolean, tableforest boolean, targetns text) xml Generates an XML Schema for the XML representation of the entire database.
database_to_xml_and_xmlschema(nulls boolean, tableforest boolean, targetns text) xml Exports both data and XML Schema for the entire database in a single XML document.