Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit12d46ac

Browse files
committed
Improve documentation about our XML functionality.
Add a section explaining how our XML features depart from currentversions of the SQL standard. Update and clarify the descriptionsof some XML functions.Chapman Flack, reviewed by Ryan LambertDiscussion:https://postgr.es/m/5BD1284C.1010305@anastigmatix.netDiscussion:https://postgr.es/m/5C81F8C0.6090901@anastigmatix.netDiscussion:https://postgr.es/m/CAN-V+g-6JqUQEQZ55Q3toXEN6d5Ez5uvzL4VR+8KtvJKj31taw@mail.gmail.com
1 parentb2b8190 commit12d46ac

File tree

4 files changed

+475
-94
lines changed

4 files changed

+475
-94
lines changed

‎doc/src/sgml/datatype.sgml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4219,6 +4219,11 @@ a0ee-bc99-9c0b-4ef8-bb6d-6bb9-bd38-0a11
42194219
value is a full document or only a content fragment.
42204220
</para>
42214221

4222+
<para>
4223+
Limits and compatibility notes for the <type>xml</type> data type
4224+
can be found in <xref linkend="xml-limits-conformance"/>.
4225+
</para>
4226+
42224227
<sect2>
42234228
<title>Creating XML Values</title>
42244229
<para>

‎doc/src/sgml/features.sgml

Lines changed: 333 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
Language SQL</quote>. A revised version of the standard is released
1717
from time to time; the most recent update appearing in 2011.
1818
The 2011 version is referred to as ISO/IEC 9075:2011, or simply as SQL:2011.
19-
The versions prior to that were SQL:2008, SQL:2003, SQL:1999, and SQL-92. Each version
19+
The versions prior to that were SQL:2008, SQL:2006, SQL:2003, SQL:1999,
20+
and SQL-92. Each version
2021
replaces the previous one, so claims of conformance to earlier
2122
versions have no official merit.
2223
<productname>PostgreSQL</productname> development aims for
@@ -155,4 +156,335 @@
155156
</para>
156157
</sect1>
157158

159+
<sect1 id="xml-limits-conformance">
160+
<title>XML Limits and Conformance to SQL/XML</title>
161+
162+
<indexterm>
163+
<primary>SQL/XML</primary>
164+
<secondary>limits and conformance</secondary>
165+
</indexterm>
166+
167+
<para>
168+
Significant revisions to the XML-related specifications in ISO/IEC 9075-14
169+
(SQL/XML) were introduced with SQL:2006.
170+
<productname>PostgreSQL</productname>'s implementation of the XML data
171+
type and related functions largely follows the earlier 2003 edition,
172+
with some borrowing from later editions. In particular:
173+
<itemizedlist>
174+
<listitem>
175+
<para>
176+
Where the current standard provides a family of XML data types
177+
to hold <quote>document</quote> or <quote>content</quote> in
178+
untyped or XML Schema-typed variants, and a type
179+
<type>XML(SEQUENCE)</type> to hold arbitrary pieces of XML content,
180+
<productname>PostgreSQL</productname> provides the single
181+
<type>xml</type> type, which can hold <quote>document</quote> or
182+
<quote>content</quote>. There is no equivalent of the
183+
standard's <quote>sequence</quote> type.
184+
</para>
185+
</listitem>
186+
187+
<listitem>
188+
<para>
189+
<productname>PostgreSQL</productname> provides two functions
190+
introduced in SQL:2006, but in variants that use the XPath 1.0
191+
language, rather than XML Query as specified for them in the
192+
standard.
193+
</para>
194+
</listitem>
195+
</itemizedlist>
196+
</para>
197+
198+
<para>
199+
This section presents some of the resulting differences you may encounter.
200+
</para>
201+
202+
<sect2 id="functions-xml-limits-xpath1">
203+
<title>Queries are restricted to XPath 1.0</title>
204+
205+
<para>
206+
The <productname>PostgreSQL</productname>-specific functions
207+
<function>xpath()</function> and <function>xpath_exists()</function>
208+
query XML documents using the XPath language.
209+
<productname>PostgreSQL</productname> also provides XPath-only variants
210+
of the standard functions <function>XMLEXISTS</function> and
211+
<function>XMLTABLE</function>, which officially use
212+
the XQuery language. For all of these functions,
213+
<productname>PostgreSQL</productname> relies on the
214+
<application>libxml2</application> library, which provides only XPath 1.0.
215+
</para>
216+
217+
<para>
218+
There is a strong connection between the XQuery language and XPath
219+
versions 2.0 and later: any expression that is syntactically valid and
220+
executes successfully in both produces the same result (with a minor
221+
exception for expressions containing numeric character references or
222+
predefined entity references, which XQuery replaces with the
223+
corresponding character while XPath leaves them alone). But there is
224+
no such connection between these languages and XPath 1.0; it was an
225+
earlier language and differs in many respects.
226+
</para>
227+
228+
<para>
229+
There are two categories of limitation to keep in mind: the restriction
230+
from XQuery to XPath for the functions specified in the SQL standard, and
231+
the restriction of XPath to version 1.0 for both the standard and the
232+
<productname>PostgreSQL</productname>-specific functions.
233+
</para>
234+
235+
<sect3>
236+
<title>Restriction of XQuery to XPath</title>
237+
238+
<para>
239+
Features of XQuery beyond those of XPath include:
240+
241+
<itemizedlist>
242+
<listitem>
243+
<para>
244+
XQuery expressions can construct and return new XML nodes, in
245+
addition to all possible XPath values. XPath can create and return
246+
values of the atomic types (numbers, strings, and so on) but can
247+
only return XML nodes that were already present in documents
248+
supplied as input to the expression.
249+
</para>
250+
</listitem>
251+
252+
<listitem>
253+
<para>
254+
XQuery has control constructs for iteration, sorting, and grouping.
255+
</para>
256+
</listitem>
257+
258+
<listitem>
259+
<para>
260+
XQuery allows declaration and use of local functions.
261+
</para>
262+
</listitem>
263+
</itemizedlist>
264+
</para>
265+
266+
<para>
267+
Recent XPath versions begin to offer capabilities overlapping with
268+
these (such as functional-style <function>for-each</function> and
269+
<function>sort</function>, anonymous functions, and
270+
<function>parse-xml</function> to create a node from a string),
271+
but such features were not available before XPath 3.0.
272+
</para>
273+
</sect3>
274+
275+
<sect3 id="xml-xpath-1-specifics">
276+
<title>Restriction of XPath to 1.0</title>
277+
278+
<para>
279+
For developers familiar with XQuery and XPath 2.0 or later, XPath 1.0
280+
presents a number of differences to contend with:
281+
282+
<itemizedlist>
283+
<listitem>
284+
<para>
285+
The fundamental type of an XQuery/XPath expression, the
286+
<type>sequence</type>, which can contain XML nodes, atomic values,
287+
or both, does not exist in XPath 1.0. A 1.0 expression can only
288+
produce a node-set (containing zero or more XML nodes), or a single
289+
atomic value.
290+
</para>
291+
</listitem>
292+
293+
<listitem>
294+
<para>
295+
Unlike an XQuery/XPath sequence, which can contain any desired
296+
items in any desired order, an XPath 1.0 node-set has no
297+
guaranteed order and, like any set, does not allow multiple
298+
appearances of the same item.
299+
<note>
300+
<para>
301+
The <application>libxml2</application> library does seem to
302+
always return node-sets to <productname>PostgreSQL</productname>
303+
with their members in the same relative order they had in the
304+
input document. Its documentation does not commit to this
305+
behavior, and an XPath 1.0 expression cannot control it.
306+
</para>
307+
</note>
308+
</para>
309+
</listitem>
310+
311+
<listitem>
312+
<para>
313+
While XQuery/XPath provides all of the types defined in XML Schema
314+
and many operators and functions over those types, XPath 1.0 has only
315+
node-sets and the three atomic types <type>boolean</type>,
316+
<type>double</type>, and <type>string</type>.
317+
</para>
318+
</listitem>
319+
320+
<listitem>
321+
<para>
322+
XPath 1.0 has no conditional operator. An XQuery/XPath expression
323+
such as <literal>if ( hat ) then hat/@size else "no hat"</literal>
324+
has no XPath 1.0 equivalent.
325+
</para>
326+
</listitem>
327+
328+
<listitem>
329+
<para>
330+
XPath 1.0 has no ordering comparison operator for strings. Both
331+
<literal>"cat" &lt; "dog"</literal> and
332+
<literal>"cat" &gt; "dog"</literal> are false, because each is a
333+
numeric comparison of two <literal>NaN</literal>s. In contrast,
334+
<literal>=</literal> and <literal>!=</literal> do compare the strings
335+
as strings.
336+
</para>
337+
</listitem>
338+
339+
<listitem>
340+
<para>
341+
XPath 1.0 blurs the distinction between
342+
<firstterm>value comparisons</firstterm> and
343+
<firstterm>general comparisons</firstterm> as XQuery/XPath define
344+
them. Both <literal>sale/@hatsize = 7</literal> and
345+
<literal>sale/@customer = "alice"</literal> are existentially
346+
quantified comparisons, true if there is
347+
any <literal>sale</literal> with the given value for the
348+
attribute, but <literal>sale/@taxable = false()</literal> is a
349+
value comparison to the
350+
<firstterm>effective boolean value</firstterm> of a whole node-set.
351+
It is true only if no <literal>sale</literal> has
352+
a <literal>taxable</literal> attribute at all.
353+
</para>
354+
</listitem>
355+
356+
<listitem>
357+
<para>
358+
In the XQuery/XPath data model, a <firstterm>document
359+
node</firstterm> can have either document form (i.e., exactly one
360+
top-level element, with only comments and processing instructions
361+
outside of it) or content form (with those constraints
362+
relaxed). Its equivalent in XPath 1.0, the
363+
<firstterm>root node</firstterm>, can only be in document form.
364+
This is part of the reason an <type>xml</type> value passed as the
365+
context item to any <productname>PostgreSQL</productname>
366+
XPath-based function must be in document form.
367+
</para>
368+
</listitem>
369+
</itemizedlist>
370+
</para>
371+
372+
<para>
373+
The differences highlighted here are not all of them. In XQuery and
374+
the 2.0 and later versions of XPath, there is an XPath 1.0 compatibility
375+
mode, and the W3C lists of
376+
<ulink url='https://www.w3.org/TR/2010/REC-xpath-functions-20101214/#xpath1-compatibility'>function library changes</ulink>
377+
and
378+
<ulink url='https://www.w3.org/TR/xpath20/#id-backwards-compatibility'>language changes</ulink>
379+
applied in that mode offer a more complete (but still not exhaustive)
380+
account of the differences. The compatibility mode cannot make the
381+
later languages exactly equivalent to XPath 1.0.
382+
</para>
383+
</sect3>
384+
385+
<sect3 id="functions-xml-limits-casts">
386+
<title>Mappings between SQL and XML data types and values</title>
387+
388+
<para>
389+
In SQL:2006 and later, both directions of conversion between standard SQL
390+
data types and the XML Schema types are specified precisely. However, the
391+
rules are expressed using the types and semantics of XQuery/XPath, and
392+
have no direct application to the different data model of XPath 1.0.
393+
</para>
394+
395+
<para>
396+
When <productname>PostgreSQL</productname> maps SQL data values to XML
397+
(as in <function>xmlelement</function>), or XML to SQL (as in the output
398+
columns of <function>xmltable</function>), except for a few cases
399+
treated specially, <productname>PostgreSQL</productname> simply assumes
400+
that the XML data type's XPath 1.0 string form will be valid as the
401+
text-input form of the SQL datatype, and conversely. This rule has the
402+
virtue of simplicity while producing, for many data types, results similar
403+
to the mappings specified in the standard.
404+
</para>
405+
406+
<para>
407+
Where interoperability with other systems is a concern, for some data
408+
types, it may be necessary to use data type formatting functions (such
409+
as those in <xref linkend="functions-formatting"/>) explicitly to
410+
produce the standard mappings.
411+
</para>
412+
</sect3>
413+
</sect2>
414+
415+
<sect2 id="functions-xml-limits-postgresql">
416+
<title>
417+
Incidental limits of the implementation
418+
</title>
419+
420+
<para>
421+
This section concerns limits that are not inherent in the
422+
<application>libxml2</application> library, but apply to the current
423+
implementation in <productname>PostgreSQL</productname>.
424+
</para>
425+
426+
<sect3>
427+
<title>Only <literal>BY VALUE</literal> passing mechanism is supported</title>
428+
429+
<para>
430+
The SQL standard defines two <firstterm>passing mechanisms</firstterm>
431+
that apply when passing an XML argument from SQL to an XML function or
432+
receiving a result: <literal>BY REF</literal>, in which a particular XML
433+
value retains its node identity, and <literal>BY VALUE</literal>, in which
434+
the content of the XML is passed but node identity is not preserved. A
435+
mechanism can be specified before a list of parameters, as the default
436+
mechanism for all of them, or after any parameter, to override the
437+
default.
438+
</para>
439+
440+
<para>
441+
To illustrate the difference, if
442+
<replaceable>x</replaceable> is an XML value, these two queries in
443+
an SQL:2006 environment would produce true and false, respectively:
444+
445+
<programlisting>
446+
SELECT XMLQUERY('$a is $b' PASSING BY REF <replaceable>x</replaceable> AS a, <replaceable>x</replaceable> AS b NULL ON EMPTY);
447+
SELECT XMLQUERY('$a is $b' PASSING BY VALUE <replaceable>x</replaceable> AS a, <replaceable>x</replaceable> AS b NULL ON EMPTY);
448+
</programlisting>
449+
</para>
450+
451+
<para>
452+
<productname>PostgreSQL</productname> will accept
453+
<literal>BY VALUE</literal> or <literal>BY REF</literal> in an
454+
<function>XMLEXISTS</function> or <function>XMLTABLE</function>
455+
construct, but it ignores them. The <type>xml</type> data type holds
456+
a character-string serialized representation, so there is no node
457+
identity to preserve, and passing is always effectively <literal>BY
458+
VALUE</literal>.
459+
</para>
460+
</sect3>
461+
462+
<sect3>
463+
<title>Cannot pass named parameters to queries</title>
464+
465+
<para>
466+
The XPath-based functions support passing one parameter to serve as the
467+
XPath expression's context item, but do not support passing additional
468+
values to be available to the expression as named parameters.
469+
</para>
470+
</sect3>
471+
472+
<sect3>
473+
<title>No <type>XML(SEQUENCE)</type> type</title>
474+
475+
<para>
476+
The <productname>PostgreSQL</productname> <type>xml</type> data type
477+
can only hold a value in <literal>DOCUMENT</literal>
478+
or <literal>CONTENT</literal> form. An XQuery/XPath expression
479+
context item must be a single XML node or atomic value, but XPath 1.0
480+
further restricts it to be only an XML node, and has no node type
481+
allowing <literal>CONTENT</literal>. The upshot is that a
482+
well-formed <literal>DOCUMENT</literal> is the only form of XML value
483+
that <productname>PostgreSQL</productname> can supply as an XPath
484+
context item.
485+
</para>
486+
</sect3>
487+
</sect2>
488+
</sect1>
489+
158490
</appendix>

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp