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.

868 lines
47KB

  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>9.14. XML Functions</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="functions-textsearch.html" title="9.13. Text Search Functions and Operators" /><link rel="next" href="functions-json.html" title="9.15. JSON Functions and Operators" /></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">9.14. XML Functions</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="functions-textsearch.html" title="9.13. Text Search Functions and Operators">Prev</a> </td><td width="10%" align="left"><a accesskey="u" href="functions.html" title="Chapter 9. Functions and Operators">Up</a></td><th width="60%" align="center">Chapter 9. Functions and Operators</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="functions-json.html" title="9.15. JSON Functions and Operators">Next</a></td></tr></table><hr></hr></div><div class="sect1" id="FUNCTIONS-XML"><div class="titlepage"><div><div><h2 class="title" style="clear: both">9.14. XML Functions</h2></div></div></div><div class="toc"><dl class="toc"><dt><span class="sect2"><a href="functions-xml.html#FUNCTIONS-PRODUCING-XML">9.14.1. Producing XML Content</a></span></dt><dt><span class="sect2"><a href="functions-xml.html#FUNCTIONS-XML-PREDICATES">9.14.2. XML Predicates</a></span></dt><dt><span class="sect2"><a href="functions-xml.html#FUNCTIONS-XML-PROCESSING">9.14.3. Processing XML</a></span></dt><dt><span class="sect2"><a href="functions-xml.html#FUNCTIONS-XML-MAPPING">9.14.4. Mapping Tables to XML</a></span></dt></dl></div><a id="id-1.5.8.19.2" class="indexterm"></a><p>
  3. The functions and function-like expressions described in this
  4. section operate on values of type <code class="type">xml</code>. See <a class="xref" href="datatype-xml.html" title="8.13. XML Type">Section 8.13</a> for information about the <code class="type">xml</code>
  5. type. The function-like expressions <code class="function">xmlparse</code>
  6. and <code class="function">xmlserialize</code> for converting to and from
  7. type <code class="type">xml</code> are documented there, not in this section.
  8. </p><p>
  9. Use of most of these functions
  10. requires <span class="productname">PostgreSQL</span> to have been built
  11. with <code class="command">configure --with-libxml</code>.
  12. </p><div class="sect2" id="FUNCTIONS-PRODUCING-XML"><div class="titlepage"><div><div><h3 class="title">9.14.1. Producing XML Content</h3></div></div></div><p>
  13. A set of functions and function-like expressions are available for
  14. producing XML content from SQL data. As such, they are
  15. particularly suitable for formatting query results into XML
  16. documents for processing in client applications.
  17. </p><div class="sect3" id="id-1.5.8.19.5.3"><div class="titlepage"><div><div><h4 class="title">9.14.1.1. <code class="literal">xmlcomment</code></h4></div></div></div><a id="id-1.5.8.19.5.3.2" class="indexterm"></a><pre class="synopsis">
  18. <code class="function">xmlcomment</code>(<em class="replaceable"><code>text</code></em>)
  19. </pre><p>
  20. The function <code class="function">xmlcomment</code> creates an XML value
  21. containing an XML comment with the specified text as content.
  22. The text cannot contain <span class="quote">“<span class="quote"><code class="literal">--</code></span>”</span> or end with a
  23. <span class="quote">“<span class="quote"><code class="literal">-</code></span>”</span> so that the resulting construct is a valid
  24. XML comment. If the argument is null, the result is null.
  25. </p><p>
  26. Example:
  27. </p><pre class="screen">
  28. SELECT xmlcomment('hello');
  29. xmlcomment
  30. --------------
  31. &lt;!--hello--&gt;
  32. </pre><p>
  33. </p></div><div class="sect3" id="id-1.5.8.19.5.4"><div class="titlepage"><div><div><h4 class="title">9.14.1.2. <code class="literal">xmlconcat</code></h4></div></div></div><a id="id-1.5.8.19.5.4.2" class="indexterm"></a><pre class="synopsis">
  34. <code class="function">xmlconcat</code>(<em class="replaceable"><code>xml</code></em>[<span class="optional">, ...</span>])
  35. </pre><p>
  36. The function <code class="function">xmlconcat</code> concatenates a list
  37. of individual XML values to create a single value containing an
  38. XML content fragment. Null values are omitted; the result is
  39. only null if there are no nonnull arguments.
  40. </p><p>
  41. Example:
  42. </p><pre class="screen">
  43. SELECT xmlconcat('&lt;abc/&gt;', '&lt;bar&gt;foo&lt;/bar&gt;');
  44. xmlconcat
  45. ----------------------
  46. &lt;abc/&gt;&lt;bar&gt;foo&lt;/bar&gt;
  47. </pre><p>
  48. </p><p>
  49. XML declarations, if present, are combined as follows. If all
  50. argument values have the same XML version declaration, that
  51. version is used in the result, else no version is used. If all
  52. argument values have the standalone declaration value
  53. <span class="quote">“<span class="quote">yes</span>”</span>, then that value is used in the result. If
  54. all argument values have a standalone declaration value and at
  55. least one is <span class="quote">“<span class="quote">no</span>”</span>, then that is used in the result.
  56. Else the result will have no standalone declaration. If the
  57. result is determined to require a standalone declaration but no
  58. version declaration, a version declaration with version 1.0 will
  59. be used because XML requires an XML declaration to contain a
  60. version declaration. Encoding declarations are ignored and
  61. removed in all cases.
  62. </p><p>
  63. Example:
  64. </p><pre class="screen">
  65. SELECT xmlconcat('&lt;?xml version="1.1"?&gt;&lt;foo/&gt;', '&lt;?xml version="1.1" standalone="no"?&gt;&lt;bar/&gt;');
  66. xmlconcat
  67. -----------------------------------
  68. &lt;?xml version="1.1"?&gt;&lt;foo/&gt;&lt;bar/&gt;
  69. </pre><p>
  70. </p></div><div class="sect3" id="id-1.5.8.19.5.5"><div class="titlepage"><div><div><h4 class="title">9.14.1.3. <code class="literal">xmlelement</code></h4></div></div></div><a id="id-1.5.8.19.5.5.2" class="indexterm"></a><pre class="synopsis">
  71. <code class="function">xmlelement</code>(name <em class="replaceable"><code>name</code></em> [<span class="optional">, xmlattributes(<em class="replaceable"><code>value</code></em> [<span class="optional">AS <em class="replaceable"><code>attname</code></em></span>] [<span class="optional">, ... </span>])</span>] [<span class="optional"><em class="replaceable"><code>, content, ...</code></em></span>])
  72. </pre><p>
  73. The <code class="function">xmlelement</code> expression produces an XML
  74. element with the given name, attributes, and content.
  75. </p><p>
  76. Examples:
  77. </p><pre class="screen">
  78. SELECT xmlelement(name foo);
  79. xmlelement
  80. ------------
  81. &lt;foo/&gt;
  82. SELECT xmlelement(name foo, xmlattributes('xyz' as bar));
  83. xmlelement
  84. ------------------
  85. &lt;foo bar="xyz"/&gt;
  86. SELECT xmlelement(name foo, xmlattributes(current_date as bar), 'cont', 'ent');
  87. xmlelement
  88. -------------------------------------
  89. &lt;foo bar="2007-01-26"&gt;content&lt;/foo&gt;
  90. </pre><p>
  91. </p><p>
  92. Element and attribute names that are not valid XML names are
  93. escaped by replacing the offending characters by the sequence
  94. <code class="literal">_x<em class="replaceable"><code>HHHH</code></em>_</code>, where
  95. <em class="replaceable"><code>HHHH</code></em> is the character's Unicode
  96. codepoint in hexadecimal notation. For example:
  97. </p><pre class="screen">
  98. SELECT xmlelement(name "foo$bar", xmlattributes('xyz' as "a&amp;b"));
  99. xmlelement
  100. ----------------------------------
  101. &lt;foo_x0024_bar a_x0026_b="xyz"/&gt;
  102. </pre><p>
  103. </p><p>
  104. An explicit attribute name need not be specified if the attribute
  105. value is a column reference, in which case the column's name will
  106. be used as the attribute name by default. In other cases, the
  107. attribute must be given an explicit name. So this example is
  108. valid:
  109. </p><pre class="screen">
  110. CREATE TABLE test (a xml, b xml);
  111. SELECT xmlelement(name test, xmlattributes(a, b)) FROM test;
  112. </pre><p>
  113. But these are not:
  114. </p><pre class="screen">
  115. SELECT xmlelement(name test, xmlattributes('constant'), a, b) FROM test;
  116. SELECT xmlelement(name test, xmlattributes(func(a, b))) FROM test;
  117. </pre><p>
  118. </p><p>
  119. Element content, if specified, will be formatted according to
  120. its data type. If the content is itself of type <code class="type">xml</code>,
  121. complex XML documents can be constructed. For example:
  122. </p><pre class="screen">
  123. SELECT xmlelement(name foo, xmlattributes('xyz' as bar),
  124. xmlelement(name abc),
  125. xmlcomment('test'),
  126. xmlelement(name xyz));
  127. xmlelement
  128. ----------------------------------------------
  129. &lt;foo bar="xyz"&gt;&lt;abc/&gt;&lt;!--test--&gt;&lt;xyz/&gt;&lt;/foo&gt;
  130. </pre><p>
  131. Content of other types will be formatted into valid XML character
  132. data. This means in particular that the characters &lt;, &gt;,
  133. and &amp; will be converted to entities. Binary data (data type
  134. <code class="type">bytea</code>) will be represented in base64 or hex
  135. encoding, depending on the setting of the configuration parameter
  136. <a class="xref" href="runtime-config-client.html#GUC-XMLBINARY">xmlbinary</a>. The particular behavior for
  137. individual data types is expected to evolve in order to align the
  138. PostgreSQL mappings with those specified in SQL:2006 and later,
  139. as discussed in <a class="xref" href="xml-limits-conformance.html#FUNCTIONS-XML-LIMITS-CASTS" title="D.3.1.3. Mappings between SQL and XML Data Types and Values">Section D.3.1.3</a>.
  140. </p></div><div class="sect3" id="id-1.5.8.19.5.6"><div class="titlepage"><div><div><h4 class="title">9.14.1.4. <code class="literal">xmlforest</code></h4></div></div></div><a id="id-1.5.8.19.5.6.2" class="indexterm"></a><pre class="synopsis">
  141. <code class="function">xmlforest</code>(<em class="replaceable"><code>content</code></em> [<span class="optional">AS <em class="replaceable"><code>name</code></em></span>] [<span class="optional">, ...</span>])
  142. </pre><p>
  143. The <code class="function">xmlforest</code> expression produces an XML
  144. forest (sequence) of elements using the given names and content.
  145. </p><p>
  146. Examples:
  147. </p><pre class="screen">
  148. SELECT xmlforest('abc' AS foo, 123 AS bar);
  149. xmlforest
  150. ------------------------------
  151. &lt;foo&gt;abc&lt;/foo&gt;&lt;bar&gt;123&lt;/bar&gt;
  152. SELECT xmlforest(table_name, column_name)
  153. FROM information_schema.columns
  154. WHERE table_schema = 'pg_catalog';
  155. xmlforest
  156. -------------------------------------------------------------------------------------------
  157. &lt;table_name&gt;pg_authid&lt;/table_name&gt;&lt;column_name&gt;rolname&lt;/column_name&gt;
  158. &lt;table_name&gt;pg_authid&lt;/table_name&gt;&lt;column_name&gt;rolsuper&lt;/column_name&gt;
  159. ...
  160. </pre><p>
  161. As seen in the second example, the element name can be omitted if
  162. the content value is a column reference, in which case the column
  163. name is used by default. Otherwise, a name must be specified.
  164. </p><p>
  165. Element names that are not valid XML names are escaped as shown
  166. for <code class="function">xmlelement</code> above. Similarly, content
  167. data is escaped to make valid XML content, unless it is already
  168. of type <code class="type">xml</code>.
  169. </p><p>
  170. Note that XML forests are not valid XML documents if they consist
  171. of more than one element, so it might be useful to wrap
  172. <code class="function">xmlforest</code> expressions in
  173. <code class="function">xmlelement</code>.
  174. </p></div><div class="sect3" id="id-1.5.8.19.5.7"><div class="titlepage"><div><div><h4 class="title">9.14.1.5. <code class="literal">xmlpi</code></h4></div></div></div><a id="id-1.5.8.19.5.7.2" class="indexterm"></a><pre class="synopsis">
  175. <code class="function">xmlpi</code>(name <em class="replaceable"><code>target</code></em> [<span class="optional">, <em class="replaceable"><code>content</code></em></span>])
  176. </pre><p>
  177. The <code class="function">xmlpi</code> expression creates an XML
  178. processing instruction. The content, if present, must not
  179. contain the character sequence <code class="literal">?&gt;</code>.
  180. </p><p>
  181. Example:
  182. </p><pre class="screen">
  183. SELECT xmlpi(name php, 'echo "hello world";');
  184. xmlpi
  185. -----------------------------
  186. &lt;?php echo "hello world";?&gt;
  187. </pre><p>
  188. </p></div><div class="sect3" id="id-1.5.8.19.5.8"><div class="titlepage"><div><div><h4 class="title">9.14.1.6. <code class="literal">xmlroot</code></h4></div></div></div><a id="id-1.5.8.19.5.8.2" class="indexterm"></a><pre class="synopsis">
  189. <code class="function">xmlroot</code>(<em class="replaceable"><code>xml</code></em>, version <em class="replaceable"><code>text</code></em> | no value [<span class="optional">, standalone yes|no|no value</span>])
  190. </pre><p>
  191. The <code class="function">xmlroot</code> expression alters the properties
  192. of the root node of an XML value. If a version is specified,
  193. it replaces the value in the root node's version declaration; if a
  194. standalone setting is specified, it replaces the value in the
  195. root node's standalone declaration.
  196. </p><p>
  197. </p><pre class="screen">
  198. SELECT xmlroot(xmlparse(document '&lt;?xml version="1.1"?&gt;&lt;content&gt;abc&lt;/content&gt;'),
  199. version '1.0', standalone yes);
  200. xmlroot
  201. ----------------------------------------
  202. &lt;?xml version="1.0" standalone="yes"?&gt;
  203. &lt;content&gt;abc&lt;/content&gt;
  204. </pre><p>
  205. </p></div><div class="sect3" id="FUNCTIONS-XML-XMLAGG"><div class="titlepage"><div><div><h4 class="title">9.14.1.7. <code class="literal">xmlagg</code></h4></div></div></div><a id="id-1.5.8.19.5.9.2" class="indexterm"></a><pre class="synopsis">
  206. <code class="function">xmlagg</code>(<em class="replaceable"><code>xml</code></em>)
  207. </pre><p>
  208. The function <code class="function">xmlagg</code> is, unlike the other
  209. functions described here, an aggregate function. It concatenates the
  210. input values to the aggregate function call,
  211. much like <code class="function">xmlconcat</code> does, except that concatenation
  212. occurs across rows rather than across expressions in a single row.
  213. See <a class="xref" href="functions-aggregate.html" title="9.20. Aggregate Functions">Section 9.20</a> for additional information
  214. about aggregate functions.
  215. </p><p>
  216. Example:
  217. </p><pre class="screen">
  218. CREATE TABLE test (y int, x xml);
  219. INSERT INTO test VALUES (1, '&lt;foo&gt;abc&lt;/foo&gt;');
  220. INSERT INTO test VALUES (2, '&lt;bar/&gt;');
  221. SELECT xmlagg(x) FROM test;
  222. xmlagg
  223. ----------------------
  224. &lt;foo&gt;abc&lt;/foo&gt;&lt;bar/&gt;
  225. </pre><p>
  226. </p><p>
  227. To determine the order of the concatenation, an <code class="literal">ORDER BY</code>
  228. clause may be added to the aggregate call as described in
  229. <a class="xref" href="sql-expressions.html#SYNTAX-AGGREGATES" title="4.2.7. Aggregate Expressions">Section 4.2.7</a>. For example:
  230. </p><pre class="screen">
  231. SELECT xmlagg(x ORDER BY y DESC) FROM test;
  232. xmlagg
  233. ----------------------
  234. &lt;bar/&gt;&lt;foo&gt;abc&lt;/foo&gt;
  235. </pre><p>
  236. </p><p>
  237. The following non-standard approach used to be recommended
  238. in previous versions, and may still be useful in specific
  239. cases:
  240. </p><pre class="screen">
  241. SELECT xmlagg(x) FROM (SELECT * FROM test ORDER BY y DESC) AS tab;
  242. xmlagg
  243. ----------------------
  244. &lt;bar/&gt;&lt;foo&gt;abc&lt;/foo&gt;
  245. </pre><p>
  246. </p></div></div><div class="sect2" id="FUNCTIONS-XML-PREDICATES"><div class="titlepage"><div><div><h3 class="title">9.14.2. XML Predicates</h3></div></div></div><p>
  247. The expressions described in this section check properties
  248. of <code class="type">xml</code> values.
  249. </p><div class="sect3" id="id-1.5.8.19.6.3"><div class="titlepage"><div><div><h4 class="title">9.14.2.1. <code class="literal">IS DOCUMENT</code></h4></div></div></div><a id="id-1.5.8.19.6.3.2" class="indexterm"></a><pre class="synopsis">
  250. <em class="replaceable"><code>xml</code></em> IS DOCUMENT
  251. </pre><p>
  252. The expression <code class="literal">IS DOCUMENT</code> returns true if the
  253. argument XML value is a proper XML document, false if it is not
  254. (that is, it is a content fragment), or null if the argument is
  255. null. See <a class="xref" href="datatype-xml.html" title="8.13. XML Type">Section 8.13</a> about the difference
  256. between documents and content fragments.
  257. </p></div><div class="sect3" id="id-1.5.8.19.6.4"><div class="titlepage"><div><div><h4 class="title">9.14.2.2. <code class="literal">IS NOT DOCUMENT</code></h4></div></div></div><a id="id-1.5.8.19.6.4.2" class="indexterm"></a><pre class="synopsis">
  258. <em class="replaceable"><code>xml</code></em> IS NOT DOCUMENT
  259. </pre><p>
  260. The expression <code class="literal">IS NOT DOCUMENT</code> returns false if the
  261. argument XML value is a proper XML document, true if it is not (that is,
  262. it is a content fragment), or null if the argument is null.
  263. </p></div><div class="sect3" id="XML-EXISTS"><div class="titlepage"><div><div><h4 class="title">9.14.2.3. <code class="literal">XMLEXISTS</code></h4></div></div></div><a id="id-1.5.8.19.6.5.2" class="indexterm"></a><pre class="synopsis">
  264. <code class="function">XMLEXISTS</code>(<em class="replaceable"><code>text</code></em> PASSING [<span class="optional">BY { REF | VALUE }</span>] <em class="replaceable"><code>xml</code></em> [<span class="optional">BY { REF | VALUE }</span>])
  265. </pre><p>
  266. The function <code class="function">xmlexists</code> evaluates an XPath 1.0
  267. expression (the first argument), with the passed XML value as its context
  268. item. The function returns false if the result of that evaluation
  269. yields an empty node-set, true if it yields any other value. The
  270. function returns null if any argument is null. A nonnull value
  271. passed as the context item must be an XML document, not a content
  272. fragment or any non-XML value.
  273. </p><p>
  274. Example:
  275. </p><pre class="screen">
  276. SELECT xmlexists('//town[text() = ''Toronto'']' PASSING BY VALUE '&lt;towns&gt;&lt;town&gt;Toronto&lt;/town&gt;&lt;town&gt;Ottawa&lt;/town&gt;&lt;/towns&gt;');
  277. xmlexists
  278. ------------
  279. t
  280. (1 row)
  281. </pre><p>
  282. </p><p>
  283. The <code class="literal">BY REF</code> and <code class="literal">BY VALUE</code> clauses
  284. are accepted in <span class="productname">PostgreSQL</span>, but are ignored,
  285. as discussed in <a class="xref" href="xml-limits-conformance.html#FUNCTIONS-XML-LIMITS-POSTGRESQL" title="D.3.2.  Incidental Limits of the Implementation">Section D.3.2</a>.
  286. In the SQL standard, the <code class="function">xmlexists</code> function
  287. evaluates an expression in the XML Query language,
  288. but <span class="productname">PostgreSQL</span> allows only an XPath 1.0
  289. expression, as discussed in
  290. <a class="xref" href="xml-limits-conformance.html#FUNCTIONS-XML-LIMITS-XPATH1" title="D.3.1. Queries Are Restricted to XPath 1.0">Section D.3.1</a>.
  291. </p></div><div class="sect3" id="XML-IS-WELL-FORMED"><div class="titlepage"><div><div><h4 class="title">9.14.2.4. <code class="literal">xml_is_well_formed</code></h4></div></div></div><a id="id-1.5.8.19.6.6.2" class="indexterm"></a><a id="id-1.5.8.19.6.6.3" class="indexterm"></a><a id="id-1.5.8.19.6.6.4" class="indexterm"></a><pre class="synopsis">
  292. <code class="function">xml_is_well_formed</code>(<em class="replaceable"><code>text</code></em>)
  293. <code class="function">xml_is_well_formed_document</code>(<em class="replaceable"><code>text</code></em>)
  294. <code class="function">xml_is_well_formed_content</code>(<em class="replaceable"><code>text</code></em>)
  295. </pre><p>
  296. These functions check whether a <code class="type">text</code> string is well-formed XML,
  297. returning a Boolean result.
  298. <code class="function">xml_is_well_formed_document</code> checks for a well-formed
  299. document, while <code class="function">xml_is_well_formed_content</code> checks
  300. for well-formed content. <code class="function">xml_is_well_formed</code> does
  301. the former if the <a class="xref" href="runtime-config-client.html#GUC-XMLOPTION">xmloption</a> configuration
  302. parameter is set to <code class="literal">DOCUMENT</code>, or the latter if it is set to
  303. <code class="literal">CONTENT</code>. This means that
  304. <code class="function">xml_is_well_formed</code> is useful for seeing whether
  305. a simple cast to type <code class="type">xml</code> will succeed, whereas the other two
  306. functions are useful for seeing whether the corresponding variants of
  307. <code class="function">XMLPARSE</code> will succeed.
  308. </p><p>
  309. Examples:
  310. </p><pre class="screen">
  311. SET xmloption TO DOCUMENT;
  312. SELECT xml_is_well_formed('&lt;&gt;');
  313. xml_is_well_formed
  314. --------------------
  315. f
  316. (1 row)
  317. SELECT xml_is_well_formed('&lt;abc/&gt;');
  318. xml_is_well_formed
  319. --------------------
  320. t
  321. (1 row)
  322. SET xmloption TO CONTENT;
  323. SELECT xml_is_well_formed('abc');
  324. xml_is_well_formed
  325. --------------------
  326. t
  327. (1 row)
  328. SELECT xml_is_well_formed_document('&lt;pg:foo xmlns:pg="http://postgresql.org/stuff"&gt;bar&lt;/pg:foo&gt;');
  329. xml_is_well_formed_document
  330. -----------------------------
  331. t
  332. (1 row)
  333. SELECT xml_is_well_formed_document('&lt;pg:foo xmlns:pg="http://postgresql.org/stuff"&gt;bar&lt;/my:foo&gt;');
  334. xml_is_well_formed_document
  335. -----------------------------
  336. f
  337. (1 row)
  338. </pre><p>
  339. The last example shows that the checks include whether
  340. namespaces are correctly matched.
  341. </p></div></div><div class="sect2" id="FUNCTIONS-XML-PROCESSING"><div class="titlepage"><div><div><h3 class="title">9.14.3. Processing XML</h3></div></div></div><p>
  342. To process values of data type <code class="type">xml</code>, PostgreSQL offers
  343. the functions <code class="function">xpath</code> and
  344. <code class="function">xpath_exists</code>, which evaluate XPath 1.0
  345. expressions, and the <code class="function">XMLTABLE</code>
  346. table function.
  347. </p><div class="sect3" id="FUNCTIONS-XML-PROCESSING-XPATH"><div class="titlepage"><div><div><h4 class="title">9.14.3.1. <code class="literal">xpath</code></h4></div></div></div><a id="id-1.5.8.19.7.3.2" class="indexterm"></a><pre class="synopsis">
  348. <code class="function">xpath</code>(<em class="replaceable"><code>xpath</code></em>, <em class="replaceable"><code>xml</code></em> [<span class="optional">, <em class="replaceable"><code>nsarray</code></em></span>])
  349. </pre><p>
  350. The function <code class="function">xpath</code> evaluates the XPath 1.0
  351. expression <em class="replaceable"><code>xpath</code></em> (a <code class="type">text</code> value)
  352. against the XML value
  353. <em class="replaceable"><code>xml</code></em>. It returns an array of XML values
  354. corresponding to the node-set produced by the XPath expression.
  355. If the XPath expression returns a scalar value rather than a node-set,
  356. a single-element array is returned.
  357. </p><p>
  358. The second argument must be a well formed XML document. In particular,
  359. it must have a single root node element.
  360. </p><p>
  361. The optional third argument of the function is an array of namespace
  362. mappings. This array should be a two-dimensional <code class="type">text</code> array with
  363. the length of the second axis being equal to 2 (i.e., it should be an
  364. array of arrays, each of which consists of exactly 2 elements).
  365. The first element of each array entry is the namespace name (alias), the
  366. second the namespace URI. It is not required that aliases provided in
  367. this array be the same as those being used in the XML document itself (in
  368. other words, both in the XML document and in the <code class="function">xpath</code>
  369. function context, aliases are <span class="emphasis"><em>local</em></span>).
  370. </p><p>
  371. Example:
  372. </p><pre class="screen">
  373. SELECT xpath('/my:a/text()', '&lt;my:a xmlns:my="http://example.com"&gt;test&lt;/my:a&gt;',
  374. ARRAY[ARRAY['my', 'http://example.com']]);
  375. xpath
  376. --------
  377. {test}
  378. (1 row)
  379. </pre><p>
  380. </p><p>
  381. To deal with default (anonymous) namespaces, do something like this:
  382. </p><pre class="screen">
  383. SELECT xpath('//mydefns:b/text()', '&lt;a xmlns="http://example.com"&gt;&lt;b&gt;test&lt;/b&gt;&lt;/a&gt;',
  384. ARRAY[ARRAY['mydefns', 'http://example.com']]);
  385. xpath
  386. --------
  387. {test}
  388. (1 row)
  389. </pre><p>
  390. </p></div><div class="sect3" id="FUNCTIONS-XML-PROCESSING-XPATH-EXISTS"><div class="titlepage"><div><div><h4 class="title">9.14.3.2. <code class="literal">xpath_exists</code></h4></div></div></div><a id="id-1.5.8.19.7.4.2" class="indexterm"></a><pre class="synopsis">
  391. <code class="function">xpath_exists</code>(<em class="replaceable"><code>xpath</code></em>, <em class="replaceable"><code>xml</code></em> [<span class="optional">, <em class="replaceable"><code>nsarray</code></em></span>])
  392. </pre><p>
  393. The function <code class="function">xpath_exists</code> is a specialized form
  394. of the <code class="function">xpath</code> function. Instead of returning the
  395. individual XML values that satisfy the XPath 1.0 expression, this function
  396. returns a Boolean indicating whether the query was satisfied or not
  397. (specifically, whether it produced any value other than an empty node-set).
  398. This function is equivalent to the <code class="literal">XMLEXISTS</code> predicate,
  399. except that it also offers support for a namespace mapping argument.
  400. </p><p>
  401. Example:
  402. </p><pre class="screen">
  403. SELECT xpath_exists('/my:a/text()', '&lt;my:a xmlns:my="http://example.com"&gt;test&lt;/my:a&gt;',
  404. ARRAY[ARRAY['my', 'http://example.com']]);
  405. xpath_exists
  406. --------------
  407. t
  408. (1 row)
  409. </pre><p>
  410. </p></div><div class="sect3" id="FUNCTIONS-XML-PROCESSING-XMLTABLE"><div class="titlepage"><div><div><h4 class="title">9.14.3.3. <code class="literal">xmltable</code></h4></div></div></div><a id="id-1.5.8.19.7.5.2" class="indexterm"></a><a id="id-1.5.8.19.7.5.3" class="indexterm"></a><pre class="synopsis">
  411. <code class="function">xmltable</code>( [<span class="optional">XMLNAMESPACES(<em class="replaceable"><code>namespace uri</code></em> AS <em class="replaceable"><code>namespace name</code></em>[<span class="optional">, ...</span>]), </span>]
  412. <em class="replaceable"><code>row_expression</code></em> PASSING [<span class="optional">BY { REF | VALUE }</span>] <em class="replaceable"><code>document_expression</code></em> [<span class="optional">BY { REF | VALUE }</span>]
  413. COLUMNS <em class="replaceable"><code>name</code></em> { <em class="replaceable"><code>type</code></em> [<span class="optional">PATH <em class="replaceable"><code>column_expression</code></em></span>] [<span class="optional">DEFAULT <em class="replaceable"><code>default_expression</code></em></span>] [<span class="optional">NOT NULL | NULL</span>]
  414. | FOR ORDINALITY }
  415. [<span class="optional">, ...</span>]
  416. )
  417. </pre><p>
  418. The <code class="function">xmltable</code> function produces a table based
  419. on the given XML value, an XPath filter to extract rows, and a
  420. set of column definitions.
  421. </p><p>
  422. The optional <code class="literal">XMLNAMESPACES</code> clause is a comma-separated
  423. list of namespaces. It specifies the XML namespaces used in
  424. the document and their aliases. A default namespace specification
  425. is not currently supported.
  426. </p><p>
  427. The required <em class="replaceable"><code>row_expression</code></em> argument is
  428. an XPath 1.0 expression that is evaluated, passing the
  429. <em class="replaceable"><code>document_expression</code></em> as its context item, to
  430. obtain a set of XML nodes. These nodes are what
  431. <code class="function">xmltable</code> transforms into output rows. No rows
  432. will be produced if the <em class="replaceable"><code>document_expression</code></em>
  433. is null, nor if the <em class="replaceable"><code>row_expression</code></em> produces
  434. an empty node-set or any value other than a node-set.
  435. </p><p>
  436. <em class="replaceable"><code>document_expression</code></em> provides the context
  437. item for the <em class="replaceable"><code>row_expression</code></em>. It must be a
  438. well-formed XML document; fragments/forests are not accepted.
  439. The <code class="literal">BY REF</code> and <code class="literal">BY VALUE</code> clauses
  440. are accepted but ignored, as discussed in
  441. <a class="xref" href="xml-limits-conformance.html#FUNCTIONS-XML-LIMITS-POSTGRESQL" title="D.3.2.  Incidental Limits of the Implementation">Section D.3.2</a>.
  442. In the SQL standard, the <code class="function">xmltable</code> function
  443. evaluates expressions in the XML Query language,
  444. but <span class="productname">PostgreSQL</span> allows only XPath 1.0
  445. expressions, as discussed in
  446. <a class="xref" href="xml-limits-conformance.html#FUNCTIONS-XML-LIMITS-XPATH1" title="D.3.1. Queries Are Restricted to XPath 1.0">Section D.3.1</a>.
  447. </p><p>
  448. The mandatory <code class="literal">COLUMNS</code> clause specifies the list
  449. of columns in the output table.
  450. Each entry describes a single column.
  451. See the syntax summary above for the format.
  452. The column name and type are required; the path, default and
  453. nullability clauses are optional.
  454. </p><p>
  455. A column marked <code class="literal">FOR ORDINALITY</code> will be populated
  456. with row numbers, starting with 1, in the order of nodes retrieved from
  457. the <em class="replaceable"><code>row_expression</code></em>'s result node-set.
  458. At most one column may be marked <code class="literal">FOR ORDINALITY</code>.
  459. </p><div class="note"><h3 class="title">Note</h3><p>
  460. XPath 1.0 does not specify an order for nodes in a node-set, so code
  461. that relies on a particular order of the results will be
  462. implementation-dependent. Details can be found in
  463. <a class="xref" href="xml-limits-conformance.html#XML-XPATH-1-SPECIFICS" title="D.3.1.2. Restriction of XPath to 1.0">Section D.3.1.2</a>.
  464. </p></div><p>
  465. The <em class="replaceable"><code>column_expression</code></em> for a column is an
  466. XPath 1.0 expression that is evaluated for each row, with the current
  467. node from the <em class="replaceable"><code>row_expression</code></em> result as its
  468. context item, to find the value of the column. If
  469. no <em class="replaceable"><code>column_expression</code></em> is given, then the
  470. column name is used as an implicit path.
  471. </p><p>
  472. If a column's XPath expression returns a non-XML value (limited to
  473. string, boolean, or double in XPath 1.0) and the column has a
  474. PostgreSQL type other than <code class="type">xml</code>, the column will be set
  475. as if by assigning the value's string representation to the PostgreSQL
  476. type. (If the value is a boolean, its string representation is taken
  477. to be <code class="literal">1</code> or <code class="literal">0</code> if the output
  478. column's type category is numeric, otherwise <code class="literal">true</code> or
  479. <code class="literal">false</code>.)
  480. </p><p>
  481. If a column's XPath expression returns a non-empty set of XML nodes
  482. and the column's PostgreSQL type is <code class="type">xml</code>, the column will
  483. be assigned the expression result exactly, if it is of document or
  484. content form.
  485. <a href="#ftn.id-1.5.8.19.7.5.14.2" class="footnote"><sup class="footnote" id="id-1.5.8.19.7.5.14.2">[8]</sup></a>
  486. </p><p>
  487. A non-XML result assigned to an <code class="type">xml</code> output column produces
  488. content, a single text node with the string value of the result.
  489. An XML result assigned to a column of any other type may not have more than
  490. one node, or an error is raised. If there is exactly one node, the column
  491. will be set as if by assigning the node's string
  492. value (as defined for the XPath 1.0 <code class="function">string</code> function)
  493. to the PostgreSQL type.
  494. </p><p>
  495. The string value of an XML element is the concatenation, in document order,
  496. of all text nodes contained in that element and its descendants. The string
  497. value of an element with no descendant text nodes is an
  498. empty string (not <code class="literal">NULL</code>).
  499. Any <code class="literal">xsi:nil</code> attributes are ignored.
  500. Note that the whitespace-only <code class="literal">text()</code> node between two non-text
  501. elements is preserved, and that leading whitespace on a <code class="literal">text()</code>
  502. node is not flattened.
  503. The XPath 1.0 <code class="function">string</code> function may be consulted for the
  504. rules defining the string value of other XML node types and non-XML values.
  505. </p><p>
  506. The conversion rules presented here are not exactly those of the SQL
  507. standard, as discussed in <a class="xref" href="xml-limits-conformance.html#FUNCTIONS-XML-LIMITS-CASTS" title="D.3.1.3. Mappings between SQL and XML Data Types and Values">Section D.3.1.3</a>.
  508. </p><p>
  509. If the path expression returns an empty node-set
  510. (typically, when it does not match)
  511. for a given row, the column will be set to <code class="literal">NULL</code>, unless
  512. a <em class="replaceable"><code>default_expression</code></em> is specified; then the
  513. value resulting from evaluating that expression is used.
  514. </p><p>
  515. Columns may be marked <code class="literal">NOT NULL</code>. If the
  516. <em class="replaceable"><code>column_expression</code></em> for a <code class="literal">NOT NULL</code> column
  517. does not match anything and there is no <code class="literal">DEFAULT</code> or the
  518. <em class="replaceable"><code>default_expression</code></em> also evaluates to null, an error
  519. is reported.
  520. </p><p>
  521. A <em class="replaceable"><code>default_expression</code></em>, rather than being
  522. evaluated immediately when <code class="function">xmltable</code> is called,
  523. is evaluated each time a default is needed for the column.
  524. If the expression qualifies as stable or immutable, the repeat
  525. evaluation may be skipped.
  526. This means that you can usefully use volatile functions like
  527. <code class="function">nextval</code> in
  528. <em class="replaceable"><code>default_expression</code></em>.
  529. </p><p>
  530. Examples:
  531. </p><pre class="screen">
  532. CREATE TABLE xmldata AS SELECT
  533. xml $$
  534. &lt;ROWS&gt;
  535. &lt;ROW id="1"&gt;
  536. &lt;COUNTRY_ID&gt;AU&lt;/COUNTRY_ID&gt;
  537. &lt;COUNTRY_NAME&gt;Australia&lt;/COUNTRY_NAME&gt;
  538. &lt;/ROW&gt;
  539. &lt;ROW id="5"&gt;
  540. &lt;COUNTRY_ID&gt;JP&lt;/COUNTRY_ID&gt;
  541. &lt;COUNTRY_NAME&gt;Japan&lt;/COUNTRY_NAME&gt;
  542. &lt;PREMIER_NAME&gt;Shinzo Abe&lt;/PREMIER_NAME&gt;
  543. &lt;SIZE unit="sq_mi"&gt;145935&lt;/SIZE&gt;
  544. &lt;/ROW&gt;
  545. &lt;ROW id="6"&gt;
  546. &lt;COUNTRY_ID&gt;SG&lt;/COUNTRY_ID&gt;
  547. &lt;COUNTRY_NAME&gt;Singapore&lt;/COUNTRY_NAME&gt;
  548. &lt;SIZE unit="sq_km"&gt;697&lt;/SIZE&gt;
  549. &lt;/ROW&gt;
  550. &lt;/ROWS&gt;
  551. $$ AS data;
  552. SELECT xmltable.*
  553. FROM xmldata,
  554. XMLTABLE('//ROWS/ROW'
  555. PASSING data
  556. COLUMNS id int PATH '@id',
  557. ordinality FOR ORDINALITY,
  558. "COUNTRY_NAME" text,
  559. country_id text PATH 'COUNTRY_ID',
  560. size_sq_km float PATH 'SIZE[@unit = "sq_km"]',
  561. size_other text PATH
  562. 'concat(SIZE[@unit!="sq_km"], " ", SIZE[@unit!="sq_km"]/@unit)',
  563. premier_name text PATH 'PREMIER_NAME' DEFAULT 'not specified') ;
  564. id | ordinality | COUNTRY_NAME | country_id | size_sq_km | size_other | premier_name
  565. ----+------------+--------------+------------+------------+--------------+---------------
  566. 1 | 1 | Australia | AU | | | not specified
  567. 5 | 2 | Japan | JP | | 145935 sq_mi | Shinzo Abe
  568. 6 | 3 | Singapore | SG | 697 | | not specified
  569. </pre><p>
  570. The following example shows concatenation of multiple text() nodes,
  571. usage of the column name as XPath filter, and the treatment of whitespace,
  572. XML comments and processing instructions:
  573. </p><pre class="screen">
  574. CREATE TABLE xmlelements AS SELECT
  575. xml $$
  576. &lt;root&gt;
  577. &lt;element&gt; Hello&lt;!-- xyxxz --&gt;2a2&lt;?aaaaa?&gt; &lt;!--x--&gt; bbb&lt;x&gt;xxx&lt;/x&gt;CC &lt;/element&gt;
  578. &lt;/root&gt;
  579. $$ AS data;
  580. SELECT xmltable.*
  581. FROM xmlelements, XMLTABLE('/root' PASSING data COLUMNS element text);
  582. element
  583. -------------------------
  584. Hello2a2 bbbxxxCC
  585. </pre><p>
  586. </p><p>
  587. The following example illustrates how
  588. the <code class="literal">XMLNAMESPACES</code> clause can be used to specify
  589. a list of namespaces
  590. used in the XML document as well as in the XPath expressions:
  591. </p><pre class="screen">
  592. WITH xmldata(data) AS (VALUES ('
  593. &lt;example xmlns="http://example.com/myns" xmlns:B="http://example.com/b"&gt;
  594. &lt;item foo="1" B:bar="2"/&gt;
  595. &lt;item foo="3" B:bar="4"/&gt;
  596. &lt;item foo="4" B:bar="5"/&gt;
  597. &lt;/example&gt;'::xml)
  598. )
  599. SELECT xmltable.*
  600. FROM XMLTABLE(XMLNAMESPACES('http://example.com/myns' AS x,
  601. 'http://example.com/b' AS "B"),
  602. '/x:example/x:item'
  603. PASSING (SELECT data FROM xmldata)
  604. COLUMNS foo int PATH '@foo',
  605. bar int PATH '@B:bar');
  606. foo | bar
  607. -----+-----
  608. 1 | 2
  609. 3 | 4
  610. 4 | 5
  611. (3 rows)
  612. </pre><p>
  613. </p></div></div><div class="sect2" id="FUNCTIONS-XML-MAPPING"><div class="titlepage"><div><div><h3 class="title">9.14.4. Mapping Tables to XML</h3></div></div></div><a id="id-1.5.8.19.8.2" class="indexterm"></a><p>
  614. The following functions map the contents of relational tables to
  615. XML values. They can be thought of as XML export functionality:
  616. </p><pre class="synopsis">
  617. table_to_xml(tbl regclass, nulls boolean, tableforest boolean, targetns text)
  618. query_to_xml(query text, nulls boolean, tableforest boolean, targetns text)
  619. cursor_to_xml(cursor refcursor, count int, nulls boolean,
  620. tableforest boolean, targetns text)
  621. </pre><p>
  622. The return type of each function is <code class="type">xml</code>.
  623. </p><p>
  624. <code class="function">table_to_xml</code> maps the content of the named
  625. table, passed as parameter <em class="parameter"><code>tbl</code></em>. The
  626. <code class="type">regclass</code> type accepts strings identifying tables using the
  627. usual notation, including optional schema qualifications and
  628. double quotes. <code class="function">query_to_xml</code> executes the
  629. query whose text is passed as parameter
  630. <em class="parameter"><code>query</code></em> and maps the result set.
  631. <code class="function">cursor_to_xml</code> fetches the indicated number of
  632. rows from the cursor specified by the parameter
  633. <em class="parameter"><code>cursor</code></em>. This variant is recommended if
  634. large tables have to be mapped, because the result value is built
  635. up in memory by each function.
  636. </p><p>
  637. If <em class="parameter"><code>tableforest</code></em> is false, then the resulting
  638. XML document looks like this:
  639. </p><pre class="screen">
  640. &lt;tablename&gt;
  641. &lt;row&gt;
  642. &lt;columnname1&gt;data&lt;/columnname1&gt;
  643. &lt;columnname2&gt;data&lt;/columnname2&gt;
  644. &lt;/row&gt;
  645. &lt;row&gt;
  646. ...
  647. &lt;/row&gt;
  648. ...
  649. &lt;/tablename&gt;
  650. </pre><p>
  651. If <em class="parameter"><code>tableforest</code></em> is true, the result is an
  652. XML content fragment that looks like this:
  653. </p><pre class="screen">
  654. &lt;tablename&gt;
  655. &lt;columnname1&gt;data&lt;/columnname1&gt;
  656. &lt;columnname2&gt;data&lt;/columnname2&gt;
  657. &lt;/tablename&gt;
  658. &lt;tablename&gt;
  659. ...
  660. &lt;/tablename&gt;
  661. ...
  662. </pre><p>
  663. If no table name is available, that is, when mapping a query or a
  664. cursor, the string <code class="literal">table</code> is used in the first
  665. format, <code class="literal">row</code> in the second format.
  666. </p><p>
  667. The choice between these formats is up to the user. The first
  668. format is a proper XML document, which will be important in many
  669. applications. The second format tends to be more useful in the
  670. <code class="function">cursor_to_xml</code> function if the result values are to be
  671. reassembled into one document later on. The functions for
  672. producing XML content discussed above, in particular
  673. <code class="function">xmlelement</code>, can be used to alter the results
  674. to taste.
  675. </p><p>
  676. The data values are mapped in the same way as described for the
  677. function <code class="function">xmlelement</code> above.
  678. </p><p>
  679. The parameter <em class="parameter"><code>nulls</code></em> determines whether null
  680. values should be included in the output. If true, null values in
  681. columns are represented as:
  682. </p><pre class="screen">
  683. &lt;columnname xsi:nil="true"/&gt;
  684. </pre><p>
  685. where <code class="literal">xsi</code> is the XML namespace prefix for XML
  686. Schema Instance. An appropriate namespace declaration will be
  687. added to the result value. If false, columns containing null
  688. values are simply omitted from the output.
  689. </p><p>
  690. The parameter <em class="parameter"><code>targetns</code></em> specifies the
  691. desired XML namespace of the result. If no particular namespace
  692. is wanted, an empty string should be passed.
  693. </p><p>
  694. The following functions return XML Schema documents describing the
  695. mappings performed by the corresponding functions above:
  696. </p><pre class="synopsis">
  697. table_to_xmlschema(tbl regclass, nulls boolean, tableforest boolean, targetns text)
  698. query_to_xmlschema(query text, nulls boolean, tableforest boolean, targetns text)
  699. cursor_to_xmlschema(cursor refcursor, nulls boolean, tableforest boolean, targetns text)
  700. </pre><p>
  701. It is essential that the same parameters are passed in order to
  702. obtain matching XML data mappings and XML Schema documents.
  703. </p><p>
  704. The following functions produce XML data mappings and the
  705. corresponding XML Schema in one document (or forest), linked
  706. together. They can be useful where self-contained and
  707. self-describing results are wanted:
  708. </p><pre class="synopsis">
  709. table_to_xml_and_xmlschema(tbl regclass, nulls boolean, tableforest boolean, targetns text)
  710. query_to_xml_and_xmlschema(query text, nulls boolean, tableforest boolean, targetns text)
  711. </pre><p>
  712. </p><p>
  713. In addition, the following functions are available to produce
  714. analogous mappings of entire schemas or the entire current
  715. database:
  716. </p><pre class="synopsis">
  717. schema_to_xml(schema name, nulls boolean, tableforest boolean, targetns text)
  718. schema_to_xmlschema(schema name, nulls boolean, tableforest boolean, targetns text)
  719. schema_to_xml_and_xmlschema(schema name, nulls boolean, tableforest boolean, targetns text)
  720. database_to_xml(nulls boolean, tableforest boolean, targetns text)
  721. database_to_xmlschema(nulls boolean, tableforest boolean, targetns text)
  722. database_to_xml_and_xmlschema(nulls boolean, tableforest boolean, targetns text)
  723. </pre><p>
  724. Note that these potentially produce a lot of data, which needs to
  725. be built up in memory. When requesting content mappings of large
  726. schemas or databases, it might be worthwhile to consider mapping the
  727. tables separately instead, possibly even through a cursor.
  728. </p><p>
  729. The result of a schema content mapping looks like this:
  730. </p><pre class="screen">
  731. &lt;schemaname&gt;
  732. table1-mapping
  733. table2-mapping
  734. ...
  735. &lt;/schemaname&gt;</pre><p>
  736. where the format of a table mapping depends on the
  737. <em class="parameter"><code>tableforest</code></em> parameter as explained above.
  738. </p><p>
  739. The result of a database content mapping looks like this:
  740. </p><pre class="screen">
  741. &lt;dbname&gt;
  742. &lt;schema1name&gt;
  743. ...
  744. &lt;/schema1name&gt;
  745. &lt;schema2name&gt;
  746. ...
  747. &lt;/schema2name&gt;
  748. ...
  749. &lt;/dbname&gt;</pre><p>
  750. where the schema mapping is as above.
  751. </p><p>
  752. As an example of using the output produced by these functions,
  753. <a class="xref" href="functions-xml.html#XSLT-XML-HTML" title="Example 9.1. XSLT Stylesheet for Converting SQL/XML Output to HTML">Example 9.1</a> shows an XSLT stylesheet that
  754. converts the output of
  755. <code class="function">table_to_xml_and_xmlschema</code> to an HTML
  756. document containing a tabular rendition of the table data. In a
  757. similar manner, the results from these functions can be
  758. converted into other XML-based formats.
  759. </p><div class="example" id="XSLT-XML-HTML"><p class="title"><strong>Example 9.1. XSLT Stylesheet for Converting SQL/XML Output to HTML</strong></p><div class="example-contents"><pre class="programlisting">
  760. &lt;?xml version="1.0"?&gt;
  761. &lt;xsl:stylesheet version="1.0"
  762. xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  763. xmlns:xsd="http://www.w3.org/2001/XMLSchema"
  764. xmlns="http://www.w3.org/1999/xhtml"
  765. &gt;
  766. &lt;xsl:output method="xml"
  767. doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"
  768. doctype-public="-//W3C/DTD XHTML 1.0 Strict//EN"
  769. indent="yes"/&gt;
  770. &lt;xsl:template match="/*"&gt;
  771. &lt;xsl:variable name="schema" select="//xsd:schema"/&gt;
  772. &lt;xsl:variable name="tabletypename"
  773. select="$schema/xsd:element[@name=name(current())]/@type"/&gt;
  774. &lt;xsl:variable name="rowtypename"
  775. select="$schema/xsd:complexType[@name=$tabletypename]/xsd:sequence/xsd:element[@name='row']/@type"/&gt;
  776. &lt;html&gt;
  777. &lt;head&gt;
  778. &lt;title&gt;&lt;xsl:value-of select="name(current())"/&gt;&lt;/title&gt;
  779. &lt;/head&gt;
  780. &lt;body&gt;
  781. &lt;table&gt;
  782. &lt;tr&gt;
  783. &lt;xsl:for-each select="$schema/xsd:complexType[@name=$rowtypename]/xsd:sequence/xsd:element/@name"&gt;
  784. &lt;th&gt;&lt;xsl:value-of select="."/&gt;&lt;/th&gt;
  785. &lt;/xsl:for-each&gt;
  786. &lt;/tr&gt;
  787. &lt;xsl:for-each select="row"&gt;
  788. &lt;tr&gt;
  789. &lt;xsl:for-each select="*"&gt;
  790. &lt;td&gt;&lt;xsl:value-of select="."/&gt;&lt;/td&gt;
  791. &lt;/xsl:for-each&gt;
  792. &lt;/tr&gt;
  793. &lt;/xsl:for-each&gt;
  794. &lt;/table&gt;
  795. &lt;/body&gt;
  796. &lt;/html&gt;
  797. &lt;/xsl:template&gt;
  798. &lt;/xsl:stylesheet&gt;
  799. </pre></div></div><br class="example-break" /></div><div class="footnotes"><br /><hr style="width:100; text-align:left;margin-left: 0" /><div id="ftn.id-1.5.8.19.7.5.14.2" class="footnote"><p><a href="#id-1.5.8.19.7.5.14.2" class="para"><sup class="para">[8] </sup></a>
  800. A result containing more than one element node at the top level, or
  801. non-whitespace text outside of an element, is an example of content form.
  802. An XPath result can be of neither form, for example if it returns an
  803. attribute node selected from the element that contains it. Such a result
  804. will be put into content form with each such disallowed node replaced by
  805. its string value, as defined for the XPath 1.0
  806. <code class="function">string</code> function.
  807. </p></div></div></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="functions-textsearch.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="functions.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="functions-json.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">9.13. Text Search Functions and Operators </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> 9.15. JSON Functions and Operators</td></tr></table></div></body></html>
上海开阖软件有限公司 沪ICP备12045867号-1