1 <?xml version="1.0" encoding="UTF-8" standalone="no"?>
2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>D.3. XML Limits and Conformance to SQL/XML</title><link rel="stylesheet" type="text/css" href="stylesheet.css" /><link rev="made" href="pgsql-docs@lists.postgresql.org" /><meta name="generator" content="DocBook XSL Stylesheets Vsnapshot" /><link rel="prev" href="unsupported-features-sql-standard.html" title="D.2. Unsupported Features" /><link rel="next" href="release.html" title="Appendix E. Release Notes" /></head><body id="docContent" class="container-fluid col-10"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="5" align="center">D.3. XML Limits and Conformance to SQL/XML</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="unsupported-features-sql-standard.html" title="D.2. Unsupported Features">Prev</a> </td><td width="10%" align="left"><a accesskey="u" href="features.html" title="Appendix D. SQL Conformance">Up</a></td><th width="60%" align="center">Appendix D. SQL Conformance</th><td width="10%" align="right"><a accesskey="h" href="index.html" title="PostgreSQL 18.0 Documentation">Home</a></td><td width="10%" align="right"> <a accesskey="n" href="release.html" title="Appendix E. Release Notes">Next</a></td></tr></table><hr /></div><div class="sect1" id="XML-LIMITS-CONFORMANCE"><div class="titlepage"><div><div><h2 class="title" style="clear: both">D.3. XML Limits and Conformance to SQL/XML <a href="#XML-LIMITS-CONFORMANCE" class="id_link">#</a></h2></div></div></div><div class="toc"><dl class="toc"><dt><span class="sect2"><a href="xml-limits-conformance.html#FUNCTIONS-XML-LIMITS-XPATH1">D.3.1. Queries Are Restricted to XPath 1.0</a></span></dt><dt><span class="sect2"><a href="xml-limits-conformance.html#FUNCTIONS-XML-LIMITS-POSTGRESQL">D.3.2. Incidental Limits of the Implementation</a></span></dt></dl></div><a id="id-1.11.5.13.2" class="indexterm"></a><p>
3 Significant revisions to the XML-related specifications in ISO/IEC 9075-14
4 (SQL/XML) were introduced with SQL:2006.
5 <span class="productname">PostgreSQL</span>'s implementation of the XML data
6 type and related functions largely follows the earlier 2003 edition,
7 with some borrowing from later editions. In particular:
8 </p><div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p>
9 Where the current standard provides a family of XML data types
10 to hold <span class="quote">“<span class="quote">document</span>”</span> or <span class="quote">“<span class="quote">content</span>”</span> in
11 untyped or XML Schema-typed variants, and a type
12 <code class="type">XML(SEQUENCE)</code> to hold arbitrary pieces of XML content,
13 <span class="productname">PostgreSQL</span> provides the single
14 <code class="type">xml</code> type, which can hold <span class="quote">“<span class="quote">document</span>”</span> or
15 <span class="quote">“<span class="quote">content</span>”</span>. There is no equivalent of the
16 standard's <span class="quote">“<span class="quote">sequence</span>”</span> type.
17 </p></li><li class="listitem"><p>
18 <span class="productname">PostgreSQL</span> provides two functions
19 introduced in SQL:2006, but in variants that use the XPath 1.0
20 language, rather than XML Query as specified for them in the
22 </p></li><li class="listitem"><p>
23 <span class="productname">PostgreSQL</span> does not support the
24 <code class="literal">RETURNING CONTENT</code> or <code class="literal">RETURNING SEQUENCE</code>
25 clauses, functions which are defined to have these in the specification
26 are implicitly returning content.
27 </p></li></ul></div><p>
29 This section presents some of the resulting differences you may encounter.
30 </p><div class="sect2" id="FUNCTIONS-XML-LIMITS-XPATH1"><div class="titlepage"><div><div><h3 class="title">D.3.1. Queries Are Restricted to XPath 1.0 <a href="#FUNCTIONS-XML-LIMITS-XPATH1" class="id_link">#</a></h3></div></div></div><p>
31 The <span class="productname">PostgreSQL</span>-specific functions
32 <code class="function">xpath()</code> and <code class="function">xpath_exists()</code>
33 query XML documents using the XPath language.
34 <span class="productname">PostgreSQL</span> also provides XPath-only variants
35 of the standard functions <code class="function">XMLEXISTS</code> and
36 <code class="function">XMLTABLE</code>, which officially use
37 the XQuery language. For all of these functions,
38 <span class="productname">PostgreSQL</span> relies on the
39 <span class="application">libxml2</span> library, which provides only XPath 1.0.
41 There is a strong connection between the XQuery language and XPath
42 versions 2.0 and later: any expression that is syntactically valid and
43 executes successfully in both produces the same result (with a minor
44 exception for expressions containing numeric character references or
45 predefined entity references, which XQuery replaces with the
46 corresponding character while XPath leaves them alone). But there is
47 no such connection between these languages and XPath 1.0; it was an
48 earlier language and differs in many respects.
50 There are two categories of limitation to keep in mind: the restriction
51 from XQuery to XPath for the functions specified in the SQL standard, and
52 the restriction of XPath to version 1.0 for both the standard and the
53 <span class="productname">PostgreSQL</span>-specific functions.
54 </p><div class="sect3" id="FUNCTIONS-XML-LIMITS-XPATH1-XQUERY-RESTRICTION"><div class="titlepage"><div><div><h4 class="title">D.3.1.1. Restriction of XQuery to XPath <a href="#FUNCTIONS-XML-LIMITS-XPATH1-XQUERY-RESTRICTION" class="id_link">#</a></h4></div></div></div><p>
55 Features of XQuery beyond those of XPath include:
57 </p><div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p>
58 XQuery expressions can construct and return new XML nodes, in
59 addition to all possible XPath values. XPath can create and return
60 values of the atomic types (numbers, strings, and so on) but can
61 only return XML nodes that were already present in documents
62 supplied as input to the expression.
63 </p></li><li class="listitem"><p>
64 XQuery has control constructs for iteration, sorting, and grouping.
65 </p></li><li class="listitem"><p>
66 XQuery allows declaration and use of local functions.
67 </p></li></ul></div><p>
69 Recent XPath versions begin to offer capabilities overlapping with
70 these (such as functional-style <code class="function">for-each</code> and
71 <code class="function">sort</code>, anonymous functions, and
72 <code class="function">parse-xml</code> to create a node from a string),
73 but such features were not available before XPath 3.0.
74 </p></div><div class="sect3" id="XML-XPATH-1-SPECIFICS"><div class="titlepage"><div><div><h4 class="title">D.3.1.2. Restriction of XPath to 1.0 <a href="#XML-XPATH-1-SPECIFICS" class="id_link">#</a></h4></div></div></div><p>
75 For developers familiar with XQuery and XPath 2.0 or later, XPath 1.0
76 presents a number of differences to contend with:
78 </p><div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p>
79 The fundamental type of an XQuery/XPath expression, the
80 <code class="type">sequence</code>, which can contain XML nodes, atomic values,
81 or both, does not exist in XPath 1.0. A 1.0 expression can only
82 produce a node-set (containing zero or more XML nodes), or a single
84 </p></li><li class="listitem"><p>
85 Unlike an XQuery/XPath sequence, which can contain any desired
86 items in any desired order, an XPath 1.0 node-set has no
87 guaranteed order and, like any set, does not allow multiple
88 appearances of the same item.
89 </p><div class="note"><h3 class="title">Note</h3><p>
90 The <span class="application">libxml2</span> library does seem to
91 always return node-sets to <span class="productname">PostgreSQL</span>
92 with their members in the same relative order they had in the
93 input document. Its documentation does not commit to this
94 behavior, and an XPath 1.0 expression cannot control it.
96 </p></li><li class="listitem"><p>
97 While XQuery/XPath provides all of the types defined in XML Schema
98 and many operators and functions over those types, XPath 1.0 has only
99 node-sets and the three atomic types <code class="type">boolean</code>,
100 <code class="type">double</code>, and <code class="type">string</code>.
101 </p></li><li class="listitem"><p>
102 XPath 1.0 has no conditional operator. An XQuery/XPath expression
103 such as <code class="literal">if ( hat ) then hat/@size else "no hat"</code>
104 has no XPath 1.0 equivalent.
105 </p></li><li class="listitem"><p>
106 XPath 1.0 has no ordering comparison operator for strings. Both
107 <code class="literal">"cat" < "dog"</code> and
108 <code class="literal">"cat" > "dog"</code> are false, because each is a
109 numeric comparison of two <code class="literal">NaN</code>s. In contrast,
110 <code class="literal">=</code> and <code class="literal">!=</code> do compare the strings
112 </p></li><li class="listitem"><p>
113 XPath 1.0 blurs the distinction between
114 <em class="firstterm">value comparisons</em> and
115 <em class="firstterm">general comparisons</em> as XQuery/XPath define
116 them. Both <code class="literal">sale/@hatsize = 7</code> and
117 <code class="literal">sale/@customer = "alice"</code> are existentially
118 quantified comparisons, true if there is
119 any <code class="literal">sale</code> with the given value for the
120 attribute, but <code class="literal">sale/@taxable = false()</code> is a
121 value comparison to the
122 <em class="firstterm">effective boolean value</em> of a whole node-set.
123 It is true only if no <code class="literal">sale</code> has
124 a <code class="literal">taxable</code> attribute at all.
125 </p></li><li class="listitem"><p>
126 In the XQuery/XPath data model, a <em class="firstterm">document
127 node</em> can have either document form (i.e., exactly one
128 top-level element, with only comments and processing instructions
129 outside of it) or content form (with those constraints
130 relaxed). Its equivalent in XPath 1.0, the
131 <em class="firstterm">root node</em>, can only be in document form.
132 This is part of the reason an <code class="type">xml</code> value passed as the
133 context item to any <span class="productname">PostgreSQL</span>
134 XPath-based function must be in document form.
135 </p></li></ul></div><p>
137 The differences highlighted here are not all of them. In XQuery and
138 the 2.0 and later versions of XPath, there is an XPath 1.0 compatibility
139 mode, and the W3C lists of
140 <a class="ulink" href="https://www.w3.org/TR/2010/REC-xpath-functions-20101214/#xpath1-compatibility" target="_top">function library changes</a>
142 <a class="ulink" href="https://www.w3.org/TR/xpath20/#id-backwards-compatibility" target="_top">language changes</a>
143 applied in that mode offer a more complete (but still not exhaustive)
144 account of the differences. The compatibility mode cannot make the
145 later languages exactly equivalent to XPath 1.0.
146 </p></div><div class="sect3" id="FUNCTIONS-XML-LIMITS-CASTS"><div class="titlepage"><div><div><h4 class="title">D.3.1.3. Mappings between SQL and XML Data Types and Values <a href="#FUNCTIONS-XML-LIMITS-CASTS" class="id_link">#</a></h4></div></div></div><p>
147 In SQL:2006 and later, both directions of conversion between standard SQL
148 data types and the XML Schema types are specified precisely. However, the
149 rules are expressed using the types and semantics of XQuery/XPath, and
150 have no direct application to the different data model of XPath 1.0.
152 When <span class="productname">PostgreSQL</span> maps SQL data values to XML
153 (as in <code class="function">xmlelement</code>), or XML to SQL (as in the output
154 columns of <code class="function">xmltable</code>), except for a few cases
155 treated specially, <span class="productname">PostgreSQL</span> simply assumes
156 that the XML data type's XPath 1.0 string form will be valid as the
157 text-input form of the SQL datatype, and conversely. This rule has the
158 virtue of simplicity while producing, for many data types, results similar
159 to the mappings specified in the standard.
161 Where interoperability with other systems is a concern, for some data
162 types, it may be necessary to use data type formatting functions (such
163 as those in <a class="xref" href="functions-formatting.html" title="9.8. Data Type Formatting Functions">Section 9.8</a>) explicitly to
164 produce the standard mappings.
165 </p></div></div><div class="sect2" id="FUNCTIONS-XML-LIMITS-POSTGRESQL"><div class="titlepage"><div><div><h3 class="title">D.3.2. Incidental Limits of the Implementation <a href="#FUNCTIONS-XML-LIMITS-POSTGRESQL" class="id_link">#</a></h3></div></div></div><p>
166 This section concerns limits that are not inherent in the
167 <span class="application">libxml2</span> library, but apply to the current
168 implementation in <span class="productname">PostgreSQL</span>.
169 </p><div class="sect3" id="FUNCTIONS-XML-LIMITS-POSTGRESQL-BY-VALUE-ONLY"><div class="titlepage"><div><div><h4 class="title">D.3.2.1. Only <code class="literal">BY VALUE</code> Passing Mechanism Is Supported <a href="#FUNCTIONS-XML-LIMITS-POSTGRESQL-BY-VALUE-ONLY" class="id_link">#</a></h4></div></div></div><p>
170 The SQL standard defines two <em class="firstterm">passing mechanisms</em>
171 that apply when passing an XML argument from SQL to an XML function or
172 receiving a result: <code class="literal">BY REF</code>, in which a particular XML
173 value retains its node identity, and <code class="literal">BY VALUE</code>, in which
174 the content of the XML is passed but node identity is not preserved. A
175 mechanism can be specified before a list of parameters, as the default
176 mechanism for all of them, or after any parameter, to override the
179 To illustrate the difference, if
180 <em class="replaceable"><code>x</code></em> is an XML value, these two queries in
181 an SQL:2006 environment would produce true and false, respectively:
183 </p><pre class="programlisting">
184 SELECT XMLQUERY('$a is $b' PASSING BY REF <em class="replaceable"><code>x</code></em> AS a, <em class="replaceable"><code>x</code></em> AS b NULL ON EMPTY);
185 SELECT XMLQUERY('$a is $b' PASSING BY VALUE <em class="replaceable"><code>x</code></em> AS a, <em class="replaceable"><code>x</code></em> AS b NULL ON EMPTY);
188 <span class="productname">PostgreSQL</span> will accept
189 <code class="literal">BY VALUE</code> or <code class="literal">BY REF</code> in an
190 <code class="function">XMLEXISTS</code> or <code class="function">XMLTABLE</code>
191 construct, but it ignores them. The <code class="type">xml</code> data type holds
192 a character-string serialized representation, so there is no node
193 identity to preserve, and passing is always effectively <code class="literal">BY
195 </p></div><div class="sect3" id="FUNCTIONS-XML-LIMITS-POSTGRESQL-NAMED-PARAMETERS"><div class="titlepage"><div><div><h4 class="title">D.3.2.2. Cannot Pass Named Parameters to Queries <a href="#FUNCTIONS-XML-LIMITS-POSTGRESQL-NAMED-PARAMETERS" class="id_link">#</a></h4></div></div></div><p>
196 The XPath-based functions support passing one parameter to serve as the
197 XPath expression's context item, but do not support passing additional
198 values to be available to the expression as named parameters.
199 </p></div><div class="sect3" id="FUNCTIONS-XML-LIMITS-POSTGRESQL-NO-XML-SEQUENCE"><div class="titlepage"><div><div><h4 class="title">D.3.2.3. No <code class="type">XML(SEQUENCE)</code> Type <a href="#FUNCTIONS-XML-LIMITS-POSTGRESQL-NO-XML-SEQUENCE" class="id_link">#</a></h4></div></div></div><p>
200 The <span class="productname">PostgreSQL</span> <code class="type">xml</code> data type
201 can only hold a value in <code class="literal">DOCUMENT</code>
202 or <code class="literal">CONTENT</code> form. An XQuery/XPath expression
203 context item must be a single XML node or atomic value, but XPath 1.0
204 further restricts it to be only an XML node, and has no node type
205 allowing <code class="literal">CONTENT</code>. The upshot is that a
206 well-formed <code class="literal">DOCUMENT</code> is the only form of XML value
207 that <span class="productname">PostgreSQL</span> can supply as an XPath
209 </p></div></div></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="unsupported-features-sql-standard.html" title="D.2. Unsupported Features">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="features.html" title="Appendix D. SQL Conformance">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="release.html" title="Appendix E. Release Notes">Next</a></td></tr><tr><td width="40%" align="left" valign="top">D.2. Unsupported Features </td><td width="20%" align="center"><a accesskey="h" href="index.html" title="PostgreSQL 18.0 Documentation">Home</a></td><td width="40%" align="right" valign="top"> Appendix E. Release Notes</td></tr></table></div></body></html>