<productname>PostgreSQL</productname>-specific functions
<function>xpath()</function> and <function>xpath_exists()</function>
query XML documents using the XPath language.
<productname>PostgreSQL</productname> also provides XPath-only variants
of the standard functions <function>XMLEXISTS</function> and
<function>XMLTABLE</function>, which officially use
the XQuery language. For all of these functions,
<productname>PostgreSQL</productname> relies on the
<application>libxml2</application> library, which provides only XPath 1.0.
</para>
<para>
There is a strong connection between the XQuery language and XPath
versions 2.0 and later: any expression that is syntactically valid and
executes successfully in both produces the same result (with a minor
exception for expressions containing numeric character references or
predefined entity references, which XQuery replaces with the
corresponding character while XPath leaves them alone). But there is
no such connection between these languages and XPath 1.0; it was an
earlier language and differs in many respects.
</para>
<para>
There are two categories of limitation to keep in mind: the restriction
from XQuery to XPath for the functions specified in the SQL standard, and
the restriction of XPath to version 1.0 for both the standard and the
<productname>PostgreSQL</productname>-specific functions.
</para>
<sect3 id="functions-xml-limits-xpath1-xquery-restriction">
<title>Restriction of XQuery to XPath</title>
<para>
Features of XQuery beyond those of XPath include:
<itemizedlist>
<listitem>
<para>
XQuery expressions can construct and return new XML nodes, in
addition to all possible XPath values. XPath can create and return
values of the atomic types (numbers, strings, and so on) but can
only return XML nodes that were already present in documents
supplied as input to the expression.
</para>
</listitem>
<listitem>
<para>
XQuery has control constructs for iteration, sorting, and grouping.
</para>
</listitem>
<listitem>
<para>
XQuery allows declaration and use of local functions.
</para>
</listitem>
</itemizedlist>
</para>
<para>
Recent XPath versions begin to offer capabilities overlapping with
these (such as functional-style <function>for-each</function> and
<function>sort</function>, anonymous functions, and
<function>parse-xml</function> to create a node from a string),
but such features were not available before XPath 3.0.
</para>
</sect3>
<sect3 id="xml-xpath-1-specifics">
<title>Restriction of XPath to 1.0</title>
<para>
For developers familiar with XQuery and XPath 2.0 or later, XPath 1.0
presents a number of differences to contend with:
<itemizedlist>
<listitem>
<para>
The fundamental type of an XQuery/XPath expression, the
<type>sequence</type>, which can contain XML nodes, atomic values,
or both, does not exist in XPath 1.0. A 1.0 expression can only
produce a node-set (containing zero or more XML nodes), or a single
atomic value.
</para>
</listitem>
<listitem>
<para>
Unlike an XQuery/XPath sequence, which can contain any desired
items in any desired order, an XPath 1.0 node-set has no
guaranteed order and, like any set, does not allow multiple
appearances of the same item.
<note>
<para>
The <application>libxml2</application> library does seem to
always return node-sets to <productname>PostgreSQL</productname>
with their members in the same relative order they had in the
input document. Its documentation