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.

291 line
18KB

  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>F.45. xml2</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="uuid-ossp.html" title="F.44. uuid-ossp" /><link rel="next" href="contrib-prog.html" title="Appendix G. Additional Supplied Programs" /></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">F.45. xml2</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="uuid-ossp.html" title="F.44. uuid-ossp">Prev</a> </td><td width="10%" align="left"><a accesskey="u" href="contrib.html" title="Appendix F. Additional Supplied Modules">Up</a></td><th width="60%" align="center">Appendix F. Additional Supplied Modules</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="contrib-prog.html" title="Appendix G. Additional Supplied Programs">Next</a></td></tr></table><hr></hr></div><div class="sect1" id="XML2"><div class="titlepage"><div><div><h2 class="title" style="clear: both">F.45. xml2</h2></div></div></div><div class="toc"><dl class="toc"><dt><span class="sect2"><a href="xml2.html#id-1.11.7.54.4">F.45.1. Deprecation Notice</a></span></dt><dt><span class="sect2"><a href="xml2.html#id-1.11.7.54.5">F.45.2. Description of Functions</a></span></dt><dt><span class="sect2"><a href="xml2.html#id-1.11.7.54.6">F.45.3. <code class="literal">xpath_table</code></a></span></dt><dt><span class="sect2"><a href="xml2.html#id-1.11.7.54.7">F.45.4. XSLT Functions</a></span></dt><dt><span class="sect2"><a href="xml2.html#id-1.11.7.54.8">F.45.5. Author</a></span></dt></dl></div><a id="id-1.11.7.54.2" class="indexterm"></a><p>
  3. The <code class="filename">xml2</code> module provides XPath querying and
  4. XSLT functionality.
  5. </p><div class="sect2" id="id-1.11.7.54.4"><div class="titlepage"><div><div><h3 class="title">F.45.1. Deprecation Notice</h3></div></div></div><p>
  6. From <span class="productname">PostgreSQL</span> 8.3 on, there is XML-related
  7. functionality based on the SQL/XML standard in the core server.
  8. That functionality covers XML syntax checking and XPath queries,
  9. which is what this module does, and more, but the API is
  10. not at all compatible. It is planned that this module will be
  11. removed in a future version of PostgreSQL in favor of the newer standard API, so
  12. you are encouraged to try converting your applications. If you
  13. find that some of the functionality of this module is not
  14. available in an adequate form with the newer API, please explain
  15. your issue to <code class="email">&lt;<a class="email" href="mailto:pgsql-hackers@lists.postgresql.org">pgsql-hackers@lists.postgresql.org</a>&gt;</code> so that the deficiency
  16. can be addressed.
  17. </p></div><div class="sect2" id="id-1.11.7.54.5"><div class="titlepage"><div><div><h3 class="title">F.45.2. Description of Functions</h3></div></div></div><p>
  18. <a class="xref" href="xml2.html#XML2-FUNCTIONS-TABLE" title="Table F.34. Functions">Table F.34</a> shows the functions provided by this module.
  19. These functions provide straightforward XML parsing and XPath queries.
  20. All arguments are of type <code class="type">text</code>, so for brevity that is not shown.
  21. </p><div class="table" id="XML2-FUNCTIONS-TABLE"><p class="title"><strong>Table F.34. Functions</strong></p><div class="table-contents"><table class="table" summary="Functions" border="1"><colgroup><col /><col /><col /></colgroup><thead><tr><th>Function</th><th>Returns</th><th>Description</th></tr></thead><tbody><tr><td>
  22. <code class="function">
  23. xml_valid(document)
  24. </code>
  25. </td><td>
  26. <code class="type">bool</code>
  27. </td><td>
  28. <p>
  29. This parses the document text in its parameter and returns true if the
  30. document is well-formed XML. (Note: this is an alias for the standard
  31. PostgreSQL function <code class="function">xml_is_well_formed()</code>. The
  32. name <code class="function">xml_valid()</code> is technically incorrect since validity
  33. and well-formedness have different meanings in XML.)
  34. </p>
  35. </td></tr><tr><td>
  36. <code class="function">
  37. xpath_string(document, query)
  38. </code>
  39. </td><td>
  40. <code class="type">text</code>
  41. </td><td rowspan="3">
  42. <p>
  43. These functions evaluate the XPath query on the supplied document, and
  44. cast the result to the specified type.
  45. </p>
  46. </td></tr><tr><td>
  47. <code class="function">
  48. xpath_number(document, query)
  49. </code>
  50. </td><td>
  51. <code class="type">float4</code>
  52. </td></tr><tr><td>
  53. <code class="function">
  54. xpath_bool(document, query)
  55. </code>
  56. </td><td>
  57. <code class="type">bool</code>
  58. </td></tr><tr><td>
  59. <code class="function">
  60. xpath_nodeset(document, query, toptag, itemtag)
  61. </code>
  62. </td><td>
  63. <code class="type">text</code>
  64. </td><td>
  65. <p>
  66. This evaluates query on document and wraps the result in XML tags. If
  67. the result is multivalued, the output will look like:
  68. </p><pre class="synopsis">
  69. &lt;toptag&gt;
  70. &lt;itemtag&gt;Value 1 which could be an XML fragment&lt;/itemtag&gt;
  71. &lt;itemtag&gt;Value 2....&lt;/itemtag&gt;
  72. &lt;/toptag&gt;
  73. </pre><p>
  74. If either <code class="literal">toptag</code> or <code class="literal">itemtag</code> is an empty string, the relevant tag is omitted.
  75. </p>
  76. </td></tr><tr><td>
  77. <code class="function">
  78. xpath_nodeset(document, query)
  79. </code>
  80. </td><td>
  81. <code class="type">text</code>
  82. </td><td>
  83. <p>
  84. Like <code class="function">xpath_nodeset(document, query, toptag, itemtag)</code> but result omits both tags.
  85. </p>
  86. </td></tr><tr><td>
  87. <code class="function">
  88. xpath_nodeset(document, query, itemtag)
  89. </code>
  90. </td><td>
  91. <code class="type">text</code>
  92. </td><td>
  93. <p>
  94. Like <code class="function">xpath_nodeset(document, query, toptag, itemtag)</code> but result omits <code class="literal">toptag</code>.
  95. </p>
  96. </td></tr><tr><td>
  97. <code class="function">
  98. xpath_list(document, query, separator)
  99. </code>
  100. </td><td>
  101. <code class="type">text</code>
  102. </td><td>
  103. <p>
  104. This function returns multiple values separated by the specified
  105. separator, for example <code class="literal">Value 1,Value 2,Value 3</code> if
  106. separator is <code class="literal">,</code>.
  107. </p>
  108. </td></tr><tr><td>
  109. <code class="function">
  110. xpath_list(document, query)
  111. </code>
  112. </td><td>
  113. <code class="type">text</code>
  114. </td><td>
  115. This is a wrapper for the above function that uses <code class="literal">,</code>
  116. as the separator.
  117. </td></tr></tbody></table></div></div><br class="table-break" /></div><div class="sect2" id="id-1.11.7.54.6"><div class="titlepage"><div><div><h3 class="title">F.45.3. <code class="literal">xpath_table</code></h3></div></div></div><a id="id-1.11.7.54.6.2" class="indexterm"></a><pre class="synopsis">
  118. xpath_table(text key, text document, text relation, text xpaths, text criteria) returns setof record
  119. </pre><p>
  120. <code class="function">xpath_table</code> is a table function that evaluates a set of XPath
  121. queries on each of a set of documents and returns the results as a
  122. table. The primary key field from the original document table is returned
  123. as the first column of the result so that the result set
  124. can readily be used in joins. The parameters are described in
  125. <a class="xref" href="xml2.html#XML2-XPATH-TABLE-PARAMETERS" title="Table F.35. xpath_table Parameters">Table F.35</a>.
  126. </p><div class="table" id="XML2-XPATH-TABLE-PARAMETERS"><p class="title"><strong>Table F.35. <code class="function">xpath_table</code> Parameters</strong></p><div class="table-contents"><table class="table" summary="xpath_table Parameters" border="1"><colgroup><col /><col /></colgroup><thead><tr><th>Parameter</th><th>Description</th></tr></thead><tbody><tr><td><em class="parameter"><code>key</code></em></td><td>
  127. <p>
  128. the name of the <span class="quote">“<span class="quote">key</span>”</span> field — this is just a field to be used as
  129. the first column of the output table, i.e., it identifies the record from
  130. which each output row came (see note below about multiple values)
  131. </p>
  132. </td></tr><tr><td><em class="parameter"><code>document</code></em></td><td>
  133. <p>
  134. the name of the field containing the XML document
  135. </p>
  136. </td></tr><tr><td><em class="parameter"><code>relation</code></em></td><td>
  137. <p>
  138. the name of the table or view containing the documents
  139. </p>
  140. </td></tr><tr><td><em class="parameter"><code>xpaths</code></em></td><td>
  141. <p>
  142. one or more XPath expressions, separated by <code class="literal">|</code>
  143. </p>
  144. </td></tr><tr><td><em class="parameter"><code>criteria</code></em></td><td>
  145. <p>
  146. the contents of the WHERE clause. This cannot be omitted, so use
  147. <code class="literal">true</code> or <code class="literal">1=1</code> if you want to
  148. process all the rows in the relation
  149. </p>
  150. </td></tr></tbody></table></div></div><br class="table-break" /><p>
  151. These parameters (except the XPath strings) are just substituted
  152. into a plain SQL SELECT statement, so you have some flexibility — the
  153. statement is
  154. </p><p>
  155. <code class="literal">
  156. SELECT &lt;key&gt;, &lt;document&gt; FROM &lt;relation&gt; WHERE &lt;criteria&gt;
  157. </code>
  158. </p><p>
  159. so those parameters can be <span class="emphasis"><em>anything</em></span> valid in those particular
  160. locations. The result from this SELECT needs to return exactly two
  161. columns (which it will unless you try to list multiple fields for key
  162. or document). Beware that this simplistic approach requires that you
  163. validate any user-supplied values to avoid SQL injection attacks.
  164. </p><p>
  165. The function has to be used in a <code class="literal">FROM</code> expression, with an
  166. <code class="literal">AS</code> clause to specify the output columns; for example
  167. </p><pre class="programlisting">
  168. SELECT * FROM
  169. xpath_table('article_id',
  170. 'article_xml',
  171. 'articles',
  172. '/article/author|/article/pages|/article/title',
  173. 'date_entered &gt; ''2003-01-01'' ')
  174. AS t(article_id integer, author text, page_count integer, title text);
  175. </pre><p>
  176. The <code class="literal">AS</code> clause defines the names and types of the columns in the
  177. output table. The first is the <span class="quote">“<span class="quote">key</span>”</span> field and the rest correspond
  178. to the XPath queries.
  179. If there are more XPath queries than result columns,
  180. the extra queries will be ignored. If there are more result columns
  181. than XPath queries, the extra columns will be NULL.
  182. </p><p>
  183. Notice that this example defines the <code class="structname">page_count</code> result
  184. column as an integer. The function deals internally with string
  185. representations, so when you say you want an integer in the output, it will
  186. take the string representation of the XPath result and use PostgreSQL input
  187. functions to transform it into an integer (or whatever type the <code class="type">AS</code>
  188. clause requests). An error will result if it can't do this — for
  189. example if the result is empty — so you may wish to just stick to
  190. <code class="type">text</code> as the column type if you think your data has any problems.
  191. </p><p>
  192. The calling <code class="command">SELECT</code> statement doesn't necessarily have to be
  193. just <code class="literal">SELECT *</code> — it can reference the output
  194. columns by name or join them to other tables. The function produces a
  195. virtual table with which you can perform any operation you wish (e.g.
  196. aggregation, joining, sorting etc). So we could also have:
  197. </p><pre class="programlisting">
  198. SELECT t.title, p.fullname, p.email
  199. FROM xpath_table('article_id', 'article_xml', 'articles',
  200. '/article/title|/article/author/@id',
  201. 'xpath_string(article_xml,''/article/@date'') &gt; ''2003-03-20'' ')
  202. AS t(article_id integer, title text, author_id integer),
  203. tblPeopleInfo AS p
  204. WHERE t.author_id = p.person_id;
  205. </pre><p>
  206. as a more complicated example. Of course, you could wrap all
  207. of this in a view for convenience.
  208. </p><div class="sect3" id="id-1.11.7.54.6.12"><div class="titlepage"><div><div><h4 class="title">F.45.3.1. Multivalued Results</h4></div></div></div><p>
  209. The <code class="function">xpath_table</code> function assumes that the results of each XPath query
  210. might be multivalued, so the number of rows returned by the function
  211. may not be the same as the number of input documents. The first row
  212. returned contains the first result from each query, the second row the
  213. second result from each query. If one of the queries has fewer values
  214. than the others, null values will be returned instead.
  215. </p><p>
  216. In some cases, a user will know that a given XPath query will return
  217. only a single result (perhaps a unique document identifier) — if used
  218. alongside an XPath query returning multiple results, the single-valued
  219. result will appear only on the first row of the result. The solution
  220. to this is to use the key field as part of a join against a simpler
  221. XPath query. As an example:
  222. </p><pre class="programlisting">
  223. CREATE TABLE test (
  224. id int PRIMARY KEY,
  225. xml text
  226. );
  227. INSERT INTO test VALUES (1, '&lt;doc num="C1"&gt;
  228. &lt;line num="L1"&gt;&lt;a&gt;1&lt;/a&gt;&lt;b&gt;2&lt;/b&gt;&lt;c&gt;3&lt;/c&gt;&lt;/line&gt;
  229. &lt;line num="L2"&gt;&lt;a&gt;11&lt;/a&gt;&lt;b&gt;22&lt;/b&gt;&lt;c&gt;33&lt;/c&gt;&lt;/line&gt;
  230. &lt;/doc&gt;');
  231. INSERT INTO test VALUES (2, '&lt;doc num="C2"&gt;
  232. &lt;line num="L1"&gt;&lt;a&gt;111&lt;/a&gt;&lt;b&gt;222&lt;/b&gt;&lt;c&gt;333&lt;/c&gt;&lt;/line&gt;
  233. &lt;line num="L2"&gt;&lt;a&gt;111&lt;/a&gt;&lt;b&gt;222&lt;/b&gt;&lt;c&gt;333&lt;/c&gt;&lt;/line&gt;
  234. &lt;/doc&gt;');
  235. SELECT * FROM
  236. xpath_table('id','xml','test',
  237. '/doc/@num|/doc/line/@num|/doc/line/a|/doc/line/b|/doc/line/c',
  238. 'true')
  239. AS t(id int, doc_num varchar(10), line_num varchar(10), val1 int, val2 int, val3 int)
  240. WHERE id = 1 ORDER BY doc_num, line_num
  241. id | doc_num | line_num | val1 | val2 | val3
  242. ----+---------+----------+------+------+------
  243. 1 | C1 | L1 | 1 | 2 | 3
  244. 1 | | L2 | 11 | 22 | 33
  245. </pre><p>
  246. </p><p>
  247. To get <code class="literal">doc_num</code> on every line, the solution is to use two invocations
  248. of <code class="function">xpath_table</code> and join the results:
  249. </p><pre class="programlisting">
  250. SELECT t.*,i.doc_num FROM
  251. xpath_table('id', 'xml', 'test',
  252. '/doc/line/@num|/doc/line/a|/doc/line/b|/doc/line/c',
  253. 'true')
  254. AS t(id int, line_num varchar(10), val1 int, val2 int, val3 int),
  255. xpath_table('id', 'xml', 'test', '/doc/@num', 'true')
  256. AS i(id int, doc_num varchar(10))
  257. WHERE i.id=t.id AND i.id=1
  258. ORDER BY doc_num, line_num;
  259. id | line_num | val1 | val2 | val3 | doc_num
  260. ----+----------+------+------+------+---------
  261. 1 | L1 | 1 | 2 | 3 | C1
  262. 1 | L2 | 11 | 22 | 33 | C1
  263. (2 rows)
  264. </pre><p>
  265. </p></div></div><div class="sect2" id="id-1.11.7.54.7"><div class="titlepage"><div><div><h3 class="title">F.45.4. XSLT Functions</h3></div></div></div><p>
  266. The following functions are available if libxslt is installed:
  267. </p><div class="sect3" id="id-1.11.7.54.7.3"><div class="titlepage"><div><div><h4 class="title">F.45.4.1. <code class="literal">xslt_process</code></h4></div></div></div><a id="id-1.11.7.54.7.3.2" class="indexterm"></a><pre class="synopsis">
  268. xslt_process(text document, text stylesheet, text paramlist) returns text
  269. </pre><p>
  270. This function applies the XSL stylesheet to the document and returns
  271. the transformed result. The <code class="literal">paramlist</code> is a list of parameter
  272. assignments to be used in the transformation, specified in the form
  273. <code class="literal">a=1,b=2</code>. Note that the
  274. parameter parsing is very simple-minded: parameter values cannot
  275. contain commas!
  276. </p><p>
  277. There is also a two-parameter version of <code class="function">xslt_process</code> which
  278. does not pass any parameters to the transformation.
  279. </p></div></div><div class="sect2" id="id-1.11.7.54.8"><div class="titlepage"><div><div><h3 class="title">F.45.5. Author</h3></div></div></div><p>
  280. John Gray <code class="email">&lt;<a class="email" href="mailto:jgray@azuli.co.uk">jgray@azuli.co.uk</a>&gt;</code>
  281. </p><p>
  282. Development of this module was sponsored by Torchbox Ltd. (www.torchbox.com).
  283. It has the same BSD license as PostgreSQL.
  284. </p></div></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="uuid-ossp.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="contrib.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="contrib-prog.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">F.44. uuid-ossp </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> Appendix G. Additional Supplied Programs</td></tr></table></div></body></html>
上海开阖软件有限公司 沪ICP备12045867号-1