@@ -4518,7 +4518,10 @@ xml '<foo>bar</foo>'
45184518 <type>xml</type>, uses the function
45194519 <function>xmlserialize</function>:<indexterm><primary>xmlserialize</primary></indexterm>
45204520<synopsis>
4521- XMLSERIALIZE ( { DOCUMENT | CONTENT } <replaceable>value</replaceable> AS <replaceable>type</replaceable> [ [ NO ] INDENT ] )
4521+ XMLSERIALIZE ( { DOCUMENT | CONTENT } <replaceable>value</replaceable> AS <replaceable>type</replaceable>
4522+ [ VERSION <replaceable>xmlversion</replaceable> ]
4523+ [ INCLUDING XMLDECLARATION | EXCLUDING XMLDECLARATION ]
4524+ [ [ NO ] INDENT ] )
45224525</synopsis>
45234526 <replaceable>type</replaceable> can be
45244527 <type>character</type>, <type>character varying</type>, or
@@ -4528,13 +4531,77 @@ XMLSERIALIZE ( { DOCUMENT | CONTENT } <replaceable>value</replaceable> AS <repla
45284531 you to simply cast the value.
45294532 </para>
45304533
4534+ <para>
4535+ The <literal>VERSION</literal> option sets the version string
4536+ used in the XML declaration. If specified, the value of
4537+ <replaceable>xmlversion</replaceable> must conform to the lexical
4538+ rules of the XML standard: it must be a string of the form
4539+ <literal>'1.'</literal> followed by one or more digits (e.g.,
4540+ <literal>'1.0'</literal>, <literal>'1.1'</literal>). Versions other
4541+ than 1.x (e.g., <literal>'2.0'</literal>) are not valid in
4542+ <literal>DOCUMENT</literal> mode and will result in an error. This format
4543+ is defined in section 2.8,
4544+ <ulink url="https://www.w3.org/TR/xml/#sec-prolog-dtd">"Prolog and Document Type Declaration"</ulink>,
4545+ of the XML standard. If the <literal>VERSION</literal> option is omitted or specified as
4546+ <literal>NULL</literal>, version <literal>1.0</literal> is used by default.
4547+
4548+ In <literal>CONTENT</literal> mode, no validation is performed on the version,
4549+ and it is emitted as specified if an XML declaration is requested.
4550+ </para>
4551+
4552+ <para>
4553+ The <literal>INCLUDING XMLDECLARATION</literal> and
4554+ <literal>EXCLUDING XMLDECLARATION</literal> options control
4555+ whether the serialized output includes an XML declaration such as
4556+ <literal><?xml version="1.0" encoding="UTF-8"?></literal>.
4557+ If neither option is specified, the presence of the declaration in
4558+ the output depends on whether the input <replaceable>value</replaceable>
4559+ originally contained one.
4560+
4561+ The <literal>INCLUDING XMLDECLARATION</literal> option is allowed in both
4562+ <literal>DOCUMENT</literal> and <literal>CONTENT</literal> modes. In
4563+ <literal>CONTENT</literal> mode, no structural validation is performed,
4564+ and the declaration is emitted according to the specified options,
4565+ even if it would not normally be considered valid by the XML specification.
4566+ </para>
4567+
45314568 <para>
45324569 The <literal>INDENT</literal> option causes the result to be
45334570 pretty-printed, while <literal>NO INDENT</literal> (which is the
45344571 default) just emits the original input string. Casting to a character
45354572 type likewise produces the original string.
45364573 </para>
45374574
4575+ <para>
4576+ Examples:
4577+ </para>
4578+ <screen><![CDATA[
4579+ SELECT
4580+ xmlserialize(
4581+ DOCUMENT xmlelement(name foo, xmlelement(name bar,42)) AS text
4582+ VERSION '1.0'
4583+ INCLUDING XMLDECLARATION
4584+ INDENT);
4585+
4586+ xmlserialize
4587+ ---------------------------------------
4588+ <?xml version="1.0" encoding="UTF8"?>+
4589+ <foo> +
4590+ <bar>42</bar> +
4591+ </foo>
4592+ (1 row)
4593+
4594+ SELECT
4595+ xmlserialize(
4596+ DOCUMENT '<?xml version="1.0" encoding="UTF-8"?><foo><bar>42</bar></foo>' AS text
4597+ EXCLUDING XMLDECLARATION);
4598+
4599+ xmlserialize
4600+ --------------------------
4601+ <foo><bar>42</bar></foo>
4602+ (1 row)
4603+ ]]></screen>
4604+
45384605 <para>
45394606 When a character string value is cast to or from type
45404607 <type>xml</type> without going through <type>XMLPARSE</type> or
0 commit comments