gooderp18绿色标准版
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

208 line
17KB

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