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.15. 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 Vsnapshot" /><link rel="prev" href="functions-uuid.html" title="9.14. UUID Functions" /><link rel="next" href="functions-json.html" title="9.16. JSON Functions and Operators" /></head><body id="docContent" class="container-fluid col-10"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="5" align="center">9.15. XML Functions</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="functions-uuid.html" title="9.14. UUID Functions">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 18.0 Documentation">Home</a></td><td width="10%" align="right"> <a accesskey="n" href="functions-json.html" title="9.16. JSON Functions and Operators">Next</a></td></tr></table><hr /></div><div class="sect1" id="FUNCTIONS-XML"><div class="titlepage"><div><div><h2 class="title" style="clear: both">9.15. XML Functions <a href="#FUNCTIONS-XML" class="id_link">#</a></h2></div></div></div><div class="toc"><dl class="toc"><dt><span class="sect2"><a href="functions-xml.html#FUNCTIONS-PRODUCING-XML">9.15.1. Producing XML Content</a></span></dt><dt><span class="sect2"><a href="functions-xml.html#FUNCTIONS-XML-PREDICATES">9.15.2. XML Predicates</a></span></dt><dt><span class="sect2"><a href="functions-xml.html#FUNCTIONS-XML-PROCESSING">9.15.3. Processing XML</a></span></dt><dt><span class="sect2"><a href="functions-xml.html#FUNCTIONS-XML-MAPPING">9.15.4. Mapping Tables to XML</a></span></dt></dl></div><a id="id-1.5.8.21.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.
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.15.1. Producing XML Content <a href="#FUNCTIONS-PRODUCING-XML" class="id_link">#</a></h3></div></div></div><p>
13 A set of functions and function-like expressions is 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="FUNCTIONS-PRODUCING-XML-XMLTEXT"><div class="titlepage"><div><div><h4 class="title">9.15.1.1. <code class="literal">xmltext</code> <a href="#FUNCTIONS-PRODUCING-XML-XMLTEXT" class="id_link">#</a></h4></div></div></div><a id="id-1.5.8.21.5.3.2" class="indexterm"></a><pre class="synopsis">
18 <code class="function">xmltext</code> ( <code class="type">text</code> ) → <code class="returnvalue">xml</code>
20 The function <code class="function">xmltext</code> returns an XML value with a single
21 text node containing the input argument as its content. Predefined entities
22 like ampersand (<code class="literal">&</code>), left and right angle brackets
23 (<code class="literal">< ></code>), and quotation marks (<code class="literal">""</code>)
27 </p><pre class="screen">
28 SELECT xmltext('< foo & bar >');
30 -------------------------
31 &lt; foo &amp; bar &gt;
33 </p></div><div class="sect3" id="FUNCTIONS-PRODUCING-XML-XMLCOMMENT"><div class="titlepage"><div><div><h4 class="title">9.15.1.2. <code class="literal">xmlcomment</code> <a href="#FUNCTIONS-PRODUCING-XML-XMLCOMMENT" class="id_link">#</a></h4></div></div></div><a id="id-1.5.8.21.5.4.2" class="indexterm"></a><pre class="synopsis">
34 <code class="function">xmlcomment</code> ( <code class="type">text</code> ) → <code class="returnvalue">xml</code>
36 The function <code class="function">xmlcomment</code> creates an XML value
37 containing an XML comment with the specified text as content.
38 The text cannot contain <span class="quote">“<span class="quote"><code class="literal">--</code></span>”</span> or end with a
39 <span class="quote">“<span class="quote"><code class="literal">-</code></span>”</span>, otherwise the resulting construct
40 would not be a valid XML comment.
41 If the argument is null, the result is null.
44 </p><pre class="screen">
45 SELECT xmlcomment('hello');
51 </p></div><div class="sect3" id="FUNCTIONS-PRODUCING-XML-XMLCONCAT"><div class="titlepage"><div><div><h4 class="title">9.15.1.3. <code class="literal">xmlconcat</code> <a href="#FUNCTIONS-PRODUCING-XML-XMLCONCAT" class="id_link">#</a></h4></div></div></div><a id="id-1.5.8.21.5.5.2" class="indexterm"></a><pre class="synopsis">
52 <code class="function">xmlconcat</code> ( <code class="type">xml</code> [<span class="optional">, ...</span>] ) → <code class="returnvalue">xml</code>
54 The function <code class="function">xmlconcat</code> concatenates a list
55 of individual XML values to create a single value containing an
56 XML content fragment. Null values are omitted; the result is
57 only null if there are no nonnull arguments.
60 </p><pre class="screen">
61 SELECT xmlconcat('<abc/>', '<bar>foo</bar>');
64 ----------------------
65 <abc/><bar>foo</bar>
68 XML declarations, if present, are combined as follows. If all
69 argument values have the same XML version declaration, that
70 version is used in the result, else no version is used. If all
71 argument values have the standalone declaration value
72 <span class="quote">“<span class="quote">yes</span>”</span>, then that value is used in the result. If
73 all argument values have a standalone declaration value and at
74 least one is <span class="quote">“<span class="quote">no</span>”</span>, then that is used in the result.
75 Else the result will have no standalone declaration. If the
76 result is determined to require a standalone declaration but no
77 version declaration, a version declaration with version 1.0 will
78 be used because XML requires an XML declaration to contain a
79 version declaration. Encoding declarations are ignored and
83 </p><pre class="screen">
84 SELECT xmlconcat('<?xml version="1.1"?><foo/>', '<?xml version="1.1" standalone="no"?><bar/>');
87 -----------------------------------
88 <?xml version="1.1"?><foo/><bar/>
90 </p></div><div class="sect3" id="FUNCTIONS-PRODUCING-XML-XMLELEMENT"><div class="titlepage"><div><div><h4 class="title">9.15.1.4. <code class="literal">xmlelement</code> <a href="#FUNCTIONS-PRODUCING-XML-XMLELEMENT" class="id_link">#</a></h4></div></div></div><a id="id-1.5.8.21.5.6.2" class="indexterm"></a><pre class="synopsis">
91 <code class="function">xmlelement</code> ( <code class="literal">NAME</code> <em class="replaceable"><code>name</code></em> [<span class="optional">, <code class="literal">XMLATTRIBUTES</code> ( <em class="replaceable"><code>attvalue</code></em> [<span class="optional"> <code class="literal">AS</code> <em class="replaceable"><code>attname</code></em> </span>] [<span class="optional">, ...</span>] ) </span>] [<span class="optional">, <em class="replaceable"><code>content</code></em> [<span class="optional">, ...</span>]</span>] ) → <code class="returnvalue">xml</code>
93 The <code class="function">xmlelement</code> expression produces an XML
94 element with the given name, attributes, and content.
95 The <em class="replaceable"><code>name</code></em>
96 and <em class="replaceable"><code>attname</code></em> items shown in the syntax are
97 simple identifiers, not values. The <em class="replaceable"><code>attvalue</code></em>
98 and <em class="replaceable"><code>content</code></em> items are expressions, which can
99 yield any <span class="productname">PostgreSQL</span> data type. The
100 argument(s) within <code class="literal">XMLATTRIBUTES</code> generate attributes
101 of the XML element; the <em class="replaceable"><code>content</code></em> value(s) are
102 concatenated to form its content.
105 </p><pre class="screen">
106 SELECT xmlelement(name foo);
112 SELECT xmlelement(name foo, xmlattributes('xyz' as bar));
116 <foo bar="xyz"/>
118 SELECT xmlelement(name foo, xmlattributes(current_date as bar), 'cont', 'ent');
121 -------------------------------------
122 <foo bar="2007-01-26">content</foo>
125 Element and attribute names that are not valid XML names are
126 escaped by replacing the offending characters by the sequence
127 <code class="literal">_x<em class="replaceable"><code>HHHH</code></em>_</code>, where
128 <em class="replaceable"><code>HHHH</code></em> is the character's Unicode
129 codepoint in hexadecimal notation. For example:
130 </p><pre class="screen">
131 SELECT xmlelement(name "foo$bar", xmlattributes('xyz' as "a&b"));
134 ----------------------------------
135 <foo_x0024_bar a_x0026_b="xyz"/>
138 An explicit attribute name need not be specified if the attribute
139 value is a column reference, in which case the column's name will
140 be used as the attribute name by default. In other cases, the
141 attribute must be given an explicit name. So this example is
143 </p><pre class="screen">
144 CREATE TABLE test (a xml, b xml);
145 SELECT xmlelement(name test, xmlattributes(a, b)) FROM test;
148 </p><pre class="screen">
149 SELECT xmlelement(name test, xmlattributes('constant'), a, b) FROM test;
150 SELECT xmlelement(name test, xmlattributes(func(a, b))) FROM test;
153 Element content, if specified, will be formatted according to
154 its data type. If the content is itself of type <code class="type">xml</code>,
155 complex XML documents can be constructed. For example:
156 </p><pre class="screen">
157 SELECT xmlelement(name foo, xmlattributes('xyz' as bar),
158 xmlelement(name abc),
160 xmlelement(name xyz));
163 ----------------------------------------------
164 <foo bar="xyz"><abc/><!--test--><xyz/></foo>
167 Content of other types will be formatted into valid XML character
168 data. This means in particular that the characters <, >,
169 and & will be converted to entities. Binary data (data type
170 <code class="type">bytea</code>) will be represented in base64 or hex
171 encoding, depending on the setting of the configuration parameter
172 <a class="xref" href="runtime-config-client.html#GUC-XMLBINARY">xmlbinary</a>. The particular behavior for
173 individual data types is expected to evolve in order to align the
174 PostgreSQL mappings with those specified in SQL:2006 and later,
175 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>.
176 </p></div><div class="sect3" id="FUNCTIONS-PRODUCING-XML-XMLFOREST"><div class="titlepage"><div><div><h4 class="title">9.15.1.5. <code class="literal">xmlforest</code> <a href="#FUNCTIONS-PRODUCING-XML-XMLFOREST" class="id_link">#</a></h4></div></div></div><a id="id-1.5.8.21.5.7.2" class="indexterm"></a><pre class="synopsis">
177 <code class="function">xmlforest</code> ( <em class="replaceable"><code>content</code></em> [<span class="optional"> <code class="literal">AS</code> <em class="replaceable"><code>name</code></em> </span>] [<span class="optional">, ...</span>] ) → <code class="returnvalue">xml</code>
179 The <code class="function">xmlforest</code> expression produces an XML
180 forest (sequence) of elements using the given names and content.
181 As for <code class="function">xmlelement</code>,
182 each <em class="replaceable"><code>name</code></em> must be a simple identifier, while
183 the <em class="replaceable"><code>content</code></em> expressions can have any data
187 </p><pre class="screen">
188 SELECT xmlforest('abc' AS foo, 123 AS bar);
191 ------------------------------
192 <foo>abc</foo><bar>123</bar>
195 SELECT xmlforest(table_name, column_name)
196 FROM information_schema.columns
197 WHERE table_schema = 'pg_catalog';
200 -----------------------------------------------------------------------
201 <table_name>pg_authid</table_name><column_name>rolname</column_name>
202 <table_name>pg_authid</table_name><column_name>rolsuper</column_name>
206 As seen in the second example, the element name can be omitted if
207 the content value is a column reference, in which case the column
208 name is used by default. Otherwise, a name must be specified.
210 Element names that are not valid XML names are escaped as shown
211 for <code class="function">xmlelement</code> above. Similarly, content
212 data is escaped to make valid XML content, unless it is already
213 of type <code class="type">xml</code>.
215 Note that XML forests are not valid XML documents if they consist
216 of more than one element, so it might be useful to wrap
217 <code class="function">xmlforest</code> expressions in
218 <code class="function">xmlelement</code>.
219 </p></div><div class="sect3" id="FUNCTIONS-PRODUCING-XML-XMLPI"><div class="titlepage"><div><div><h4 class="title">9.15.1.6. <code class="literal">xmlpi</code> <a href="#FUNCTIONS-PRODUCING-XML-XMLPI" class="id_link">#</a></h4></div></div></div><a id="id-1.5.8.21.5.8.2" class="indexterm"></a><pre class="synopsis">
220 <code class="function">xmlpi</code> ( <code class="literal">NAME</code> <em class="replaceable"><code>name</code></em> [<span class="optional">, <em class="replaceable"><code>content</code></em> </span>] ) → <code class="returnvalue">xml</code>
222 The <code class="function">xmlpi</code> expression creates an XML
223 processing instruction.
224 As for <code class="function">xmlelement</code>,
225 the <em class="replaceable"><code>name</code></em> must be a simple identifier, while
226 the <em class="replaceable"><code>content</code></em> expression can have any data type.
227 The <em class="replaceable"><code>content</code></em>, if present, must not contain the
228 character sequence <code class="literal">?></code>.
231 </p><pre class="screen">
232 SELECT xmlpi(name php, 'echo "hello world";');
235 -----------------------------
236 <?php echo "hello world";?>
238 </p></div><div class="sect3" id="FUNCTIONS-PRODUCING-XML-XMLROOT"><div class="titlepage"><div><div><h4 class="title">9.15.1.7. <code class="literal">xmlroot</code> <a href="#FUNCTIONS-PRODUCING-XML-XMLROOT" class="id_link">#</a></h4></div></div></div><a id="id-1.5.8.21.5.9.2" class="indexterm"></a><pre class="synopsis">
239 <code class="function">xmlroot</code> ( <code class="type">xml</code>, <code class="literal">VERSION</code> {<code class="type">text</code>|<code class="literal">NO VALUE</code>} [<span class="optional">, <code class="literal">STANDALONE</code> {<code class="literal">YES</code>|<code class="literal">NO</code>|<code class="literal">NO VALUE</code>} </span>] ) → <code class="returnvalue">xml</code>
241 The <code class="function">xmlroot</code> expression alters the properties
242 of the root node of an XML value. If a version is specified,
243 it replaces the value in the root node's version declaration; if a
244 standalone setting is specified, it replaces the value in the
245 root node's standalone declaration.
247 </p><pre class="screen">
248 SELECT xmlroot(xmlparse(document '<?xml version="1.1"?><content>abc</content>'),
249 version '1.0', standalone yes);
252 ----------------------------------------
253 <?xml version="1.0" standalone="yes"?>
254 <content>abc</content>
256 </p></div><div class="sect3" id="FUNCTIONS-XML-XMLAGG"><div class="titlepage"><div><div><h4 class="title">9.15.1.8. <code class="literal">xmlagg</code> <a href="#FUNCTIONS-XML-XMLAGG" class="id_link">#</a></h4></div></div></div><a id="id-1.5.8.21.5.10.2" class="indexterm"></a><pre class="synopsis">
257 <code class="function">xmlagg</code> ( <code class="type">xml</code> ) → <code class="returnvalue">xml</code>
259 The function <code class="function">xmlagg</code> is, unlike the other
260 functions described here, an aggregate function. It concatenates the
261 input values to the aggregate function call,
262 much like <code class="function">xmlconcat</code> does, except that concatenation
263 occurs across rows rather than across expressions in a single row.
264 See <a class="xref" href="functions-aggregate.html" title="9.21. Aggregate Functions">Section 9.21</a> for additional information
265 about aggregate functions.
268 </p><pre class="screen">
269 CREATE TABLE test (y int, x xml);
270 INSERT INTO test VALUES (1, '<foo>abc</foo>');
271 INSERT INTO test VALUES (2, '<bar/>');
272 SELECT xmlagg(x) FROM test;
274 ----------------------
275 <foo>abc</foo><bar/>
278 To determine the order of the concatenation, an <code class="literal">ORDER BY</code>
279 clause may be added to the aggregate call as described in
280 <a class="xref" href="sql-expressions.html#SYNTAX-AGGREGATES" title="4.2.7. Aggregate Expressions">Section 4.2.7</a>. For example:
282 </p><pre class="screen">
283 SELECT xmlagg(x ORDER BY y DESC) FROM test;
285 ----------------------
286 <bar/><foo>abc</foo>
289 The following non-standard approach used to be recommended
290 in previous versions, and may still be useful in specific
293 </p><pre class="screen">
294 SELECT xmlagg(x) FROM (SELECT * FROM test ORDER BY y DESC) AS tab;
296 ----------------------
297 <bar/><foo>abc</foo>
299 </p></div></div><div class="sect2" id="FUNCTIONS-XML-PREDICATES"><div class="titlepage"><div><div><h3 class="title">9.15.2. XML Predicates <a href="#FUNCTIONS-XML-PREDICATES" class="id_link">#</a></h3></div></div></div><p>
300 The expressions described in this section check properties
301 of <code class="type">xml</code> values.
302 </p><div class="sect3" id="FUNCTIONS-PRODUCING-XML-IS-DOCUMENT"><div class="titlepage"><div><div><h4 class="title">9.15.2.1. <code class="literal">IS DOCUMENT</code> <a href="#FUNCTIONS-PRODUCING-XML-IS-DOCUMENT" class="id_link">#</a></h4></div></div></div><a id="id-1.5.8.21.6.3.2" class="indexterm"></a><pre class="synopsis">
303 <code class="type">xml</code> <code class="literal">IS DOCUMENT</code> → <code class="returnvalue">boolean</code>
305 The expression <code class="literal">IS DOCUMENT</code> returns true if the
306 argument XML value is a proper XML document, false if it is not
307 (that is, it is a content fragment), or null if the argument is
308 null. See <a class="xref" href="datatype-xml.html" title="8.13. XML Type">Section 8.13</a> about the difference
309 between documents and content fragments.
310 </p></div><div class="sect3" id="FUNCTIONS-PRODUCING-XML-IS-NOT-DOCUMENT"><div class="titlepage"><div><div><h4 class="title">9.15.2.2. <code class="literal">IS NOT DOCUMENT</code> <a href="#FUNCTIONS-PRODUCING-XML-IS-NOT-DOCUMENT" class="id_link">#</a></h4></div></div></div><a id="id-1.5.8.21.6.4.2" class="indexterm"></a><pre class="synopsis">
311 <code class="type">xml</code> <code class="literal">IS NOT DOCUMENT</code> → <code class="returnvalue">boolean</code>
313 The expression <code class="literal">IS NOT DOCUMENT</code> returns false if the
314 argument XML value is a proper XML document, true if it is not (that is,
315 it is a content fragment), or null if the argument is null.
316 </p></div><div class="sect3" id="XML-EXISTS"><div class="titlepage"><div><div><h4 class="title">9.15.2.3. <code class="literal">XMLEXISTS</code> <a href="#XML-EXISTS" class="id_link">#</a></h4></div></div></div><a id="id-1.5.8.21.6.5.2" class="indexterm"></a><pre class="synopsis">
317 <code class="function">XMLEXISTS</code> ( <code class="type">text</code> <code class="literal">PASSING</code> [<span class="optional"><code class="literal">BY</code> {<code class="literal">REF</code>|<code class="literal">VALUE</code>}</span>] <code class="type">xml</code> [<span class="optional"><code class="literal">BY</code> {<code class="literal">REF</code>|<code class="literal">VALUE</code>}</span>] ) → <code class="returnvalue">boolean</code>
319 The function <code class="function">xmlexists</code> evaluates an XPath 1.0
320 expression (the first argument), with the passed XML value as its context
321 item. The function returns false if the result of that evaluation
322 yields an empty node-set, true if it yields any other value. The
323 function returns null if any argument is null. A nonnull value
324 passed as the context item must be an XML document, not a content
325 fragment or any non-XML value.
328 </p><pre class="screen">
329 SELECT xmlexists('//town[text() = ''Toronto'']' PASSING BY VALUE '<towns><town>Toronto</town><town>Ottawa</town></towns>');
337 The <code class="literal">BY REF</code> and <code class="literal">BY VALUE</code> clauses
338 are accepted in <span class="productname">PostgreSQL</span>, but are ignored,
339 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>.
341 In the SQL standard, the <code class="function">xmlexists</code> function
342 evaluates an expression in the XML Query language,
343 but <span class="productname">PostgreSQL</span> allows only an XPath 1.0
344 expression, as discussed in
345 <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>.
346 </p></div><div class="sect3" id="XML-IS-WELL-FORMED"><div class="titlepage"><div><div><h4 class="title">9.15.2.4. <code class="literal">xml_is_well_formed</code> <a href="#XML-IS-WELL-FORMED" class="id_link">#</a></h4></div></div></div><a id="id-1.5.8.21.6.6.2" class="indexterm"></a><a id="id-1.5.8.21.6.6.3" class="indexterm"></a><a id="id-1.5.8.21.6.6.4" class="indexterm"></a><pre class="synopsis">
347 <code class="function">xml_is_well_formed</code> ( <code class="type">text</code> ) → <code class="returnvalue">boolean</code>
348 <code class="function">xml_is_well_formed_document</code> ( <code class="type">text</code> ) → <code class="returnvalue">boolean</code>
349 <code class="function">xml_is_well_formed_content</code> ( <code class="type">text</code> ) → <code class="returnvalue">boolean</code>
351 These functions check whether a <code class="type">text</code> string represents
352 well-formed XML, returning a Boolean result.
353 <code class="function">xml_is_well_formed_document</code> checks for a well-formed
354 document, while <code class="function">xml_is_well_formed_content</code> checks
355 for well-formed content. <code class="function">xml_is_well_formed</code> does
356 the former if the <a class="xref" href="runtime-config-client.html#GUC-XMLOPTION">xmloption</a> configuration
357 parameter is set to <code class="literal">DOCUMENT</code>, or the latter if it is set to
358 <code class="literal">CONTENT</code>. This means that
359 <code class="function">xml_is_well_formed</code> is useful for seeing whether
360 a simple cast to type <code class="type">xml</code> will succeed, whereas the other two
361 functions are useful for seeing whether the corresponding variants of
362 <code class="function">XMLPARSE</code> will succeed.
366 </p><pre class="screen">
367 SET xmloption TO DOCUMENT;
368 SELECT xml_is_well_formed('<>');
374 SELECT xml_is_well_formed('<abc/>');
380 SET xmloption TO CONTENT;
381 SELECT xml_is_well_formed('abc');
387 SELECT xml_is_well_formed_document('<pg:foo xmlns:pg="http://postgresql.org/stuff">bar</pg:foo>');
388 xml_is_well_formed_document
389 -----------------------------
393 SELECT xml_is_well_formed_document('<pg:foo xmlns:pg="http://postgresql.org/stuff">bar</my:foo>');
394 xml_is_well_formed_document
395 -----------------------------
400 The last example shows that the checks include whether
401 namespaces are correctly matched.
402 </p></div></div><div class="sect2" id="FUNCTIONS-XML-PROCESSING"><div class="titlepage"><div><div><h3 class="title">9.15.3. Processing XML <a href="#FUNCTIONS-XML-PROCESSING" class="id_link">#</a></h3></div></div></div><p>
403 To process values of data type <code class="type">xml</code>, PostgreSQL offers
404 the functions <code class="function">xpath</code> and
405 <code class="function">xpath_exists</code>, which evaluate XPath 1.0
406 expressions, and the <code class="function">XMLTABLE</code>
408 </p><div class="sect3" id="FUNCTIONS-XML-PROCESSING-XPATH"><div class="titlepage"><div><div><h4 class="title">9.15.3.1. <code class="literal">xpath</code> <a href="#FUNCTIONS-XML-PROCESSING-XPATH" class="id_link">#</a></h4></div></div></div><a id="id-1.5.8.21.7.3.2" class="indexterm"></a><pre class="synopsis">
409 <code class="function">xpath</code> ( <em class="parameter"><code>xpath</code></em> <code class="type">text</code>, <em class="parameter"><code>xml</code></em> <code class="type">xml</code> [<span class="optional">, <em class="parameter"><code>nsarray</code></em> <code class="type">text[]</code> </span>] ) → <code class="returnvalue">xml[]</code>
411 The function <code class="function">xpath</code> evaluates the XPath 1.0
412 expression <em class="parameter"><code>xpath</code></em> (given as text)
413 against the XML value
414 <em class="parameter"><code>xml</code></em>. It returns an array of XML values
415 corresponding to the node-set produced by the XPath expression.
416 If the XPath expression returns a scalar value rather than a node-set,
417 a single-element array is returned.
419 The second argument must be a well formed XML document. In particular,
420 it must have a single root node element.
422 The optional third argument of the function is an array of namespace
423 mappings. This array should be a two-dimensional <code class="type">text</code> array with
424 the length of the second axis being equal to 2 (i.e., it should be an
425 array of arrays, each of which consists of exactly 2 elements).
426 The first element of each array entry is the namespace name (alias), the
427 second the namespace URI. It is not required that aliases provided in
428 this array be the same as those being used in the XML document itself (in
429 other words, both in the XML document and in the <code class="function">xpath</code>
430 function context, aliases are <span class="emphasis"><em>local</em></span>).
433 </p><pre class="screen">
434 SELECT xpath('/my:a/text()', '<my:a xmlns:my="http://example.com">test</my:a>',
435 ARRAY[ARRAY['my', 'http://example.com']]);
443 To deal with default (anonymous) namespaces, do something like this:
444 </p><pre class="screen">
445 SELECT xpath('//mydefns:b/text()', '<a xmlns="http://example.com"><b>test</b></a>',
446 ARRAY[ARRAY['mydefns', 'http://example.com']]);
453 </p></div><div class="sect3" id="FUNCTIONS-XML-PROCESSING-XPATH-EXISTS"><div class="titlepage"><div><div><h4 class="title">9.15.3.2. <code class="literal">xpath_exists</code> <a href="#FUNCTIONS-XML-PROCESSING-XPATH-EXISTS" class="id_link">#</a></h4></div></div></div><a id="id-1.5.8.21.7.4.2" class="indexterm"></a><pre class="synopsis">
454 <code class="function">xpath_exists</code> ( <em class="parameter"><code>xpath</code></em> <code class="type">text</code>, <em class="parameter"><code>xml</code></em> <code class="type">xml</code> [<span class="optional">, <em class="parameter"><code>nsarray</code></em> <code class="type">text[]</code> </span>] ) → <code class="returnvalue">boolean</code>
456 The function <code class="function">xpath_exists</code> is a specialized form
457 of the <code class="function">xpath</code> function. Instead of returning the
458 individual XML values that satisfy the XPath 1.0 expression, this function
459 returns a Boolean indicating whether the query was satisfied or not
460 (specifically, whether it produced any value other than an empty node-set).
461 This function is equivalent to the <code class="literal">XMLEXISTS</code> predicate,
462 except that it also offers support for a namespace mapping argument.
465 </p><pre class="screen">
466 SELECT xpath_exists('/my:a/text()', '<my:a xmlns:my="http://example.com">test</my:a>',
467 ARRAY[ARRAY['my', 'http://example.com']]);
474 </p></div><div class="sect3" id="FUNCTIONS-XML-PROCESSING-XMLTABLE"><div class="titlepage"><div><div><h4 class="title">9.15.3.3. <code class="literal">xmltable</code> <a href="#FUNCTIONS-XML-PROCESSING-XMLTABLE" class="id_link">#</a></h4></div></div></div><a id="id-1.5.8.21.7.5.2" class="indexterm"></a><a id="id-1.5.8.21.7.5.3" class="indexterm"></a><pre class="synopsis">
475 <code class="function">XMLTABLE</code> (
476 [<span class="optional"> <code class="literal">XMLNAMESPACES</code> ( <em class="replaceable"><code>namespace_uri</code></em> <code class="literal">AS</code> <em class="replaceable"><code>namespace_name</code></em> [<span class="optional">, ...</span>] ), </span>]
477 <em class="replaceable"><code>row_expression</code></em> <code class="literal">PASSING</code> [<span class="optional"><code class="literal">BY</code> {<code class="literal">REF</code>|<code class="literal">VALUE</code>}</span>] <em class="replaceable"><code>document_expression</code></em> [<span class="optional"><code class="literal">BY</code> {<code class="literal">REF</code>|<code class="literal">VALUE</code>}</span>]
478 <code class="literal">COLUMNS</code> <em class="replaceable"><code>name</code></em> { <em class="replaceable"><code>type</code></em> [<span class="optional"><code class="literal">PATH</code> <em class="replaceable"><code>column_expression</code></em></span>] [<span class="optional"><code class="literal">DEFAULT</code> <em class="replaceable"><code>default_expression</code></em></span>] [<span class="optional"><code class="literal">NOT NULL</code> | <code class="literal">NULL</code></span>]
479 | <code class="literal">FOR ORDINALITY</code> }
480 [<span class="optional">, ...</span>]
481 ) → <code class="returnvalue">setof record</code>
483 The <code class="function">xmltable</code> expression produces a table based
484 on an XML value, an XPath filter to extract rows, and a
485 set of column definitions.
486 Although it syntactically resembles a function, it can only appear
487 as a table in a query's <code class="literal">FROM</code> clause.
489 The optional <code class="literal">XMLNAMESPACES</code> clause gives a
490 comma-separated list of namespace definitions, where
491 each <em class="replaceable"><code>namespace_uri</code></em> is a <code class="type">text</code>
492 expression and each <em class="replaceable"><code>namespace_name</code></em> is a simple
493 identifier. It specifies the XML namespaces used in the document and
494 their aliases. A default namespace specification is not currently
497 The required <em class="replaceable"><code>row_expression</code></em> argument is an
498 XPath 1.0 expression (given as <code class="type">text</code>) that is evaluated,
499 passing the XML value <em class="replaceable"><code>document_expression</code></em> as
500 its context item, to obtain a set of XML nodes. These nodes are what
501 <code class="function">xmltable</code> transforms into output rows. No rows
502 will be produced if the <em class="replaceable"><code>document_expression</code></em>
503 is null, nor if the <em class="replaceable"><code>row_expression</code></em> produces
504 an empty node-set or any value other than a node-set.
506 <em class="replaceable"><code>document_expression</code></em> provides the context
507 item for the <em class="replaceable"><code>row_expression</code></em>. It must be a
508 well-formed XML document; fragments/forests are not accepted.
509 The <code class="literal">BY REF</code> and <code class="literal">BY VALUE</code> clauses
510 are accepted but ignored, as discussed in
511 <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>.
513 In the SQL standard, the <code class="function">xmltable</code> function
514 evaluates expressions in the XML Query language,
515 but <span class="productname">PostgreSQL</span> allows only XPath 1.0
516 expressions, as discussed in
517 <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>.
519 The required <code class="literal">COLUMNS</code> clause specifies the
520 column(s) that will be produced in the output table.
521 See the syntax summary above for the format.
522 A name is required for each column, as is a data type
523 (unless <code class="literal">FOR ORDINALITY</code> is specified, in which case
524 type <code class="type">integer</code> is implicit). The path, default and
525 nullability clauses are optional.
527 A column marked <code class="literal">FOR ORDINALITY</code> will be populated
528 with row numbers, starting with 1, in the order of nodes retrieved from
529 the <em class="replaceable"><code>row_expression</code></em>'s result node-set.
530 At most one column may be marked <code class="literal">FOR ORDINALITY</code>.
531 </p><div class="note"><h3 class="title">Note</h3><p>
532 XPath 1.0 does not specify an order for nodes in a node-set, so code
533 that relies on a particular order of the results will be
534 implementation-dependent. Details can be found in
535 <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>.
537 The <em class="replaceable"><code>column_expression</code></em> for a column is an
538 XPath 1.0 expression that is evaluated for each row, with the current
539 node from the <em class="replaceable"><code>row_expression</code></em> result as its
540 context item, to find the value of the column. If
541 no <em class="replaceable"><code>column_expression</code></em> is given, then the
542 column name is used as an implicit path.
544 If a column's XPath expression returns a non-XML value (which is limited
545 to string, boolean, or double in XPath 1.0) and the column has a
546 PostgreSQL type other than <code class="type">xml</code>, the column will be set
547 as if by assigning the value's string representation to the PostgreSQL
548 type. (If the value is a boolean, its string representation is taken
549 to be <code class="literal">1</code> or <code class="literal">0</code> if the output
550 column's type category is numeric, otherwise <code class="literal">true</code> or
551 <code class="literal">false</code>.)
553 If a column's XPath expression returns a non-empty set of XML nodes
554 and the column's PostgreSQL type is <code class="type">xml</code>, the column will
555 be assigned the expression result exactly, if it is of document or
557 <a href="#ftn.id-1.5.8.21.7.5.15.2" class="footnote"><sup class="footnote" id="id-1.5.8.21.7.5.15.2">[8]</sup></a>
559 A non-XML result assigned to an <code class="type">xml</code> output column produces
560 content, a single text node with the string value of the result.
561 An XML result assigned to a column of any other type may not have more than
562 one node, or an error is raised. If there is exactly one node, the column
563 will be set as if by assigning the node's string
564 value (as defined for the XPath 1.0 <code class="function">string</code> function)
565 to the PostgreSQL type.
567 The string value of an XML element is the concatenation, in document order,
568 of all text nodes contained in that element and its descendants. The string
569 value of an element with no descendant text nodes is an
570 empty string (not <code class="literal">NULL</code>).
571 Any <code class="literal">xsi:nil</code> attributes are ignored.
572 Note that the whitespace-only <code class="literal">text()</code> node between two non-text
573 elements is preserved, and that leading whitespace on a <code class="literal">text()</code>
574 node is not flattened.
575 The XPath 1.0 <code class="function">string</code> function may be consulted for the
576 rules defining the string value of other XML node types and non-XML values.
578 The conversion rules presented here are not exactly those of the SQL
579 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>.
581 If the path expression returns an empty node-set
582 (typically, when it does not match)
583 for a given row, the column will be set to <code class="literal">NULL</code>, unless
584 a <em class="replaceable"><code>default_expression</code></em> is specified; then the
585 value resulting from evaluating that expression is used.
587 A <em class="replaceable"><code>default_expression</code></em>, rather than being
588 evaluated immediately when <code class="function">xmltable</code> is called,
589 is evaluated each time a default is needed for the column.
590 If the expression qualifies as stable or immutable, the repeat
591 evaluation may be skipped.
592 This means that you can usefully use volatile functions like
593 <code class="function">nextval</code> in
594 <em class="replaceable"><code>default_expression</code></em>.
596 Columns may be marked <code class="literal">NOT NULL</code>. If the
597 <em class="replaceable"><code>column_expression</code></em> for a <code class="literal">NOT
598 NULL</code> column does not match anything and there is
599 no <code class="literal">DEFAULT</code> or
600 the <em class="replaceable"><code>default_expression</code></em> also evaluates to null,
601 an error is reported.
604 </p><pre class="screen">
605 CREATE TABLE xmldata AS SELECT
609 <COUNTRY_ID>AU</COUNTRY_ID>
610 <COUNTRY_NAME>Australia</COUNTRY_NAME>
613 <COUNTRY_ID>JP</COUNTRY_ID>
614 <COUNTRY_NAME>Japan</COUNTRY_NAME>
615 <PREMIER_NAME>Shinzo Abe</PREMIER_NAME>
616 <SIZE unit="sq_mi">145935</SIZE>
619 <COUNTRY_ID>SG</COUNTRY_ID>
620 <COUNTRY_NAME>Singapore</COUNTRY_NAME>
621 <SIZE unit="sq_km">697</SIZE>
628 XMLTABLE('//ROWS/ROW'
630 COLUMNS id int PATH '@id',
631 ordinality FOR ORDINALITY,
633 country_id text PATH 'COUNTRY_ID',
634 size_sq_km float PATH 'SIZE[@unit = "sq_km"]',
636 'concat(SIZE[@unit!="sq_km"], " ", SIZE[@unit!="sq_km"]/@unit)',
637 premier_name text PATH 'PREMIER_NAME' DEFAULT 'not specified');
639 id | ordinality | COUNTRY_NAME | country_id | size_sq_km | size_other | premier_name
640 ----+------------+--------------+------------+------------+--------------+---------------
641 1 | 1 | Australia | AU | | | not specified
642 5 | 2 | Japan | JP | | 145935 sq_mi | Shinzo Abe
643 6 | 3 | Singapore | SG | 697 | | not specified
646 The following example shows concatenation of multiple text() nodes,
647 usage of the column name as XPath filter, and the treatment of whitespace,
648 XML comments and processing instructions:
650 </p><pre class="screen">
651 CREATE TABLE xmlelements AS SELECT
654 <element> Hello<!-- xyxxz -->2a2<?aaaaa?> <!--x--> bbb<x>xxx</x>CC </element>
659 FROM xmlelements, XMLTABLE('/root' PASSING data COLUMNS element text);
661 -------------------------
665 The following example illustrates how
666 the <code class="literal">XMLNAMESPACES</code> clause can be used to specify
668 used in the XML document as well as in the XPath expressions:
670 </p><pre class="screen">
671 WITH xmldata(data) AS (VALUES ('
672 <example xmlns="http://example.com/myns" xmlns:B="http://example.com/b">
673 <item foo="1" B:bar="2"/>
674 <item foo="3" B:bar="4"/>
675 <item foo="4" B:bar="5"/>
676 </example>'::xml)
679 FROM XMLTABLE(XMLNAMESPACES('http://example.com/myns' AS x,
680 'http://example.com/b' AS "B"),
682 PASSING (SELECT data FROM xmldata)
683 COLUMNS foo int PATH '@foo',
684 bar int PATH '@B:bar');
692 </p></div></div><div class="sect2" id="FUNCTIONS-XML-MAPPING"><div class="titlepage"><div><div><h3 class="title">9.15.4. Mapping Tables to XML <a href="#FUNCTIONS-XML-MAPPING" class="id_link">#</a></h3></div></div></div><a id="id-1.5.8.21.8.2" class="indexterm"></a><p>
693 The following functions map the contents of relational tables to
694 XML values. They can be thought of as XML export functionality:
695 </p><pre class="synopsis">
696 <code class="function">table_to_xml</code> ( <em class="parameter"><code>table</code></em> <code class="type">regclass</code>, <em class="parameter"><code>nulls</code></em> <code class="type">boolean</code>,
697 <em class="parameter"><code>tableforest</code></em> <code class="type">boolean</code>, <em class="parameter"><code>targetns</code></em> <code class="type">text</code> ) → <code class="returnvalue">xml</code>
698 <code class="function">query_to_xml</code> ( <em class="parameter"><code>query</code></em> <code class="type">text</code>, <em class="parameter"><code>nulls</code></em> <code class="type">boolean</code>,
699 <em class="parameter"><code>tableforest</code></em> <code class="type">boolean</code>, <em class="parameter"><code>targetns</code></em> <code class="type">text</code> ) → <code class="returnvalue">xml</code>
700 <code class="function">cursor_to_xml</code> ( <em class="parameter"><code>cursor</code></em> <code class="type">refcursor</code>, <em class="parameter"><code>count</code></em> <code class="type">integer</code>, <em class="parameter"><code>nulls</code></em> <code class="type">boolean</code>,
701 <em class="parameter"><code>tableforest</code></em> <code class="type">boolean</code>, <em class="parameter"><code>targetns</code></em> <code class="type">text</code> ) → <code class="returnvalue">xml</code>
704 <code class="function">table_to_xml</code> maps the content of the named
705 table, passed as parameter <em class="parameter"><code>table</code></em>. The
706 <code class="type">regclass</code> type accepts strings identifying tables using the
707 usual notation, including optional schema qualification and
708 double quotes (see <a class="xref" href="datatype-oid.html" title="8.19. Object Identifier Types">Section 8.19</a> for details).
709 <code class="function">query_to_xml</code> executes the
710 query whose text is passed as parameter
711 <em class="parameter"><code>query</code></em> and maps the result set.
712 <code class="function">cursor_to_xml</code> fetches the indicated number of
713 rows from the cursor specified by the parameter
714 <em class="parameter"><code>cursor</code></em>. This variant is recommended if
715 large tables have to be mapped, because the result value is built
716 up in memory by each function.
718 If <em class="parameter"><code>tableforest</code></em> is false, then the resulting
719 XML document looks like this:
720 </p><pre class="screen">
723 <columnname1>data</columnname1>
724 <columnname2>data</columnname2>
735 If <em class="parameter"><code>tableforest</code></em> is true, the result is an
736 XML content fragment that looks like this:
737 </p><pre class="screen">
739 <columnname1>data</columnname1>
740 <columnname2>data</columnname2>
750 If no table name is available, that is, when mapping a query or a
751 cursor, the string <code class="literal">table</code> is used in the first
752 format, <code class="literal">row</code> in the second format.
754 The choice between these formats is up to the user. The first
755 format is a proper XML document, which will be important in many
756 applications. The second format tends to be more useful in the
757 <code class="function">cursor_to_xml</code> function if the result values are to be
758 reassembled into one document later on. The functions for
759 producing XML content discussed above, in particular
760 <code class="function">xmlelement</code>, can be used to alter the results
763 The data values are mapped in the same way as described for the
764 function <code class="function">xmlelement</code> above.
766 The parameter <em class="parameter"><code>nulls</code></em> determines whether null
767 values should be included in the output. If true, null values in
768 columns are represented as:
769 </p><pre class="screen">
770 <columnname xsi:nil="true"/>
772 where <code class="literal">xsi</code> is the XML namespace prefix for XML
773 Schema Instance. An appropriate namespace declaration will be
774 added to the result value. If false, columns containing null
775 values are simply omitted from the output.
777 The parameter <em class="parameter"><code>targetns</code></em> specifies the
778 desired XML namespace of the result. If no particular namespace
779 is wanted, an empty string should be passed.
781 The following functions return XML Schema documents describing the
782 mappings performed by the corresponding functions above:
783 </p><pre class="synopsis">
784 <code class="function">table_to_xmlschema</code> ( <em class="parameter"><code>table</code></em> <code class="type">regclass</code>, <em class="parameter"><code>nulls</code></em> <code class="type">boolean</code>,
785 <em class="parameter"><code>tableforest</code></em> <code class="type">boolean</code>, <em class="parameter"><code>targetns</code></em> <code class="type">text</code> ) → <code class="returnvalue">xml</code>
786 <code class="function">query_to_xmlschema</code> ( <em class="parameter"><code>query</code></em> <code class="type">text</code>, <em class="parameter"><code>nulls</code></em> <code class="type">boolean</code>,
787 <em class="parameter"><code>tableforest</code></em> <code class="type">boolean</code>, <em class="parameter"><code>targetns</code></em> <code class="type">text</code> ) → <code class="returnvalue">xml</code>
788 <code class="function">cursor_to_xmlschema</code> ( <em class="parameter"><code>cursor</code></em> <code class="type">refcursor</code>, <em class="parameter"><code>nulls</code></em> <code class="type">boolean</code>,
789 <em class="parameter"><code>tableforest</code></em> <code class="type">boolean</code>, <em class="parameter"><code>targetns</code></em> <code class="type">text</code> ) → <code class="returnvalue">xml</code>
791 It is essential that the same parameters are passed in order to
792 obtain matching XML data mappings and XML Schema documents.
794 The following functions produce XML data mappings and the
795 corresponding XML Schema in one document (or forest), linked
796 together. They can be useful where self-contained and
797 self-describing results are wanted:
798 </p><pre class="synopsis">
799 <code class="function">table_to_xml_and_xmlschema</code> ( <em class="parameter"><code>table</code></em> <code class="type">regclass</code>, <em class="parameter"><code>nulls</code></em> <code class="type">boolean</code>,
800 <em class="parameter"><code>tableforest</code></em> <code class="type">boolean</code>, <em class="parameter"><code>targetns</code></em> <code class="type">text</code> ) → <code class="returnvalue">xml</code>
801 <code class="function">query_to_xml_and_xmlschema</code> ( <em class="parameter"><code>query</code></em> <code class="type">text</code>, <em class="parameter"><code>nulls</code></em> <code class="type">boolean</code>,
802 <em class="parameter"><code>tableforest</code></em> <code class="type">boolean</code>, <em class="parameter"><code>targetns</code></em> <code class="type">text</code> ) → <code class="returnvalue">xml</code>
805 In addition, the following functions are available to produce
806 analogous mappings of entire schemas or the entire current
808 </p><pre class="synopsis">
809 <code class="function">schema_to_xml</code> ( <em class="parameter"><code>schema</code></em> <code class="type">name</code>, <em class="parameter"><code>nulls</code></em> <code class="type">boolean</code>,
810 <em class="parameter"><code>tableforest</code></em> <code class="type">boolean</code>, <em class="parameter"><code>targetns</code></em> <code class="type">text</code> ) → <code class="returnvalue">xml</code>
811 <code class="function">schema_to_xmlschema</code> ( <em class="parameter"><code>schema</code></em> <code class="type">name</code>, <em class="parameter"><code>nulls</code></em> <code class="type">boolean</code>,
812 <em class="parameter"><code>tableforest</code></em> <code class="type">boolean</code>, <em class="parameter"><code>targetns</code></em> <code class="type">text</code> ) → <code class="returnvalue">xml</code>
813 <code class="function">schema_to_xml_and_xmlschema</code> ( <em class="parameter"><code>schema</code></em> <code class="type">name</code>, <em class="parameter"><code>nulls</code></em> <code class="type">boolean</code>,
814 <em class="parameter"><code>tableforest</code></em> <code class="type">boolean</code>, <em class="parameter"><code>targetns</code></em> <code class="type">text</code> ) → <code class="returnvalue">xml</code>
816 <code class="function">database_to_xml</code> ( <em class="parameter"><code>nulls</code></em> <code class="type">boolean</code>,
817 <em class="parameter"><code>tableforest</code></em> <code class="type">boolean</code>, <em class="parameter"><code>targetns</code></em> <code class="type">text</code> ) → <code class="returnvalue">xml</code>
818 <code class="function">database_to_xmlschema</code> ( <em class="parameter"><code>nulls</code></em> <code class="type">boolean</code>,
819 <em class="parameter"><code>tableforest</code></em> <code class="type">boolean</code>, <em class="parameter"><code>targetns</code></em> <code class="type">text</code> ) → <code class="returnvalue">xml</code>
820 <code class="function">database_to_xml_and_xmlschema</code> ( <em class="parameter"><code>nulls</code></em> <code class="type">boolean</code>,
821 <em class="parameter"><code>tableforest</code></em> <code class="type">boolean</code>, <em class="parameter"><code>targetns</code></em> <code class="type">text</code> ) → <code class="returnvalue">xml</code>
824 These functions ignore tables that are not readable by the current user.
825 The database-wide functions additionally ignore schemas that the current
826 user does not have <code class="literal">USAGE</code> (lookup) privilege for.
828 Note that these potentially produce a lot of data, which needs to
829 be built up in memory. When requesting content mappings of large
830 schemas or databases, it might be worthwhile to consider mapping the
831 tables separately instead, possibly even through a cursor.
833 The result of a schema content mapping looks like this:
835 </p><pre class="screen">
844 </schemaname></pre><p>
846 where the format of a table mapping depends on the
847 <em class="parameter"><code>tableforest</code></em> parameter as explained above.
849 The result of a database content mapping looks like this:
851 </p><pre class="screen">
864 </dbname></pre><p>
866 where the schema mapping is as above.
868 As an example of using the output produced by these functions,
869 <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
870 converts the output of
871 <code class="function">table_to_xml_and_xmlschema</code> to an HTML
872 document containing a tabular rendition of the table data. In a
873 similar manner, the results from these functions can be
874 converted into other XML-based formats.
875 </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">
876 <?xml version="1.0"?>
877 <xsl:stylesheet version="1.0"
878 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
879 xmlns:xsd="http://www.w3.org/2001/XMLSchema"
880 xmlns="http://www.w3.org/1999/xhtml"
883 <xsl:output method="xml"
884 doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"
885 doctype-public="-//W3C/DTD XHTML 1.0 Strict//EN"
888 <xsl:template match="/*">
889 <xsl:variable name="schema" select="//xsd:schema"/>
890 <xsl:variable name="tabletypename"
891 select="$schema/xsd:element[@name=name(current())]/@type"/>
892 <xsl:variable name="rowtypename"
893 select="$schema/xsd:complexType[@name=$tabletypename]/xsd:sequence/xsd:element[@name='row']/@type"/>
897 <title><xsl:value-of select="name(current())"/></title>
902 <xsl:for-each select="$schema/xsd:complexType[@name=$rowtypename]/xsd:sequence/xsd:element/@name">
903 <th><xsl:value-of select="."/></th>
904 </xsl:for-each>
907 <xsl:for-each select="row">
909 <xsl:for-each select="*">
910 <td><xsl:value-of select="."/></td>
911 </xsl:for-each>
913 </xsl:for-each>
917 </xsl:template>
919 </xsl:stylesheet>
920 </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.21.7.5.15.2" class="footnote"><p><a href="#id-1.5.8.21.7.5.15.2" class="para"><sup class="para">[8] </sup></a>
921 A result containing more than one element node at the top level, or
922 non-whitespace text outside of an element, is an example of content form.
923 An XPath result can be of neither form, for example if it returns an
924 attribute node selected from the element that contains it. Such a result
925 will be put into content form with each such disallowed node replaced by
926 its string value, as defined for the XPath 1.0
927 <code class="function">string</code> function.
928 </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-uuid.html" title="9.14. UUID Functions">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="functions.html" title="Chapter 9. Functions and Operators">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="functions-json.html" title="9.16. JSON Functions and Operators">Next</a></td></tr><tr><td width="40%" align="left" valign="top">9.14. UUID Functions </td><td width="20%" align="center"><a accesskey="h" href="index.html" title="PostgreSQL 18.0 Documentation">Home</a></td><td width="40%" align="right" valign="top"> 9.16. JSON Functions and Operators</td></tr></table></div></body></html>