Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1evMQ4-00063T-Jw for pgsql-general@arkaria.postgresql.org; Mon, 12 Mar 2018 12:19:12 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1evMQ3-0007ha-Cr for pgsql-general@arkaria.postgresql.org; Mon, 12 Mar 2018 12:19:11 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1evMQ3-0007hQ-4y for pgsql-general@lists.postgresql.org; Mon, 12 Mar 2018 12:19:11 +0000 Received: from [195.159.176.226] (helo=blaine.gmane.org) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1evMPz-0005Fp-UW for pgsql-general@postgresql.org; Mon, 12 Mar 2018 12:19:10 +0000 Received: from list by blaine.gmane.org with local (Exim 4.84_2) (envelope-from ) id 1evMNt-0007I6-MT for pgsql-general@postgresql.org; Mon, 12 Mar 2018 13:16:57 +0100 X-Injected-Via-Gmane: http://gmane.org/ To: pgsql-general@postgresql.org From: Thomas Kellerer Subject: query_to_xml() returns invalid XML when query returns no rows Date: Mon, 12 Mar 2018 13:18:58 +0100 Lines: 25 Message-ID: Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit X-Complaints-To: usenet@blaine.gmane.org User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.23) Gecko/20090812 Thunderbird/2.0.0.23 Mnenhy/0.7.6.666 X-Mozilla-News-Host: news://news.gmane.org:119 Content-Language: en-GB X-Host-Lookup-Failed: Reverse DNS lookup failed for 195.159.176.226 (failed) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk I am not sure if this qualifies as a bug: query_to_xml() returns an empty XML document when the query returns no rows, e.g: select query_to_xml('select 42 where false', false, true, ''); The problem with this is, that if the resulting XML is then fed into e.g. the xpath() function, that function fails because the "empty" document is an invalid XML: So the following query: select xpath('/row/col/text()', query_to_xml('select 42 as col where false', false, true, '')); fails with "ERROR: could not parse XML document" I would have expected query_to_xml() to return NULL if the query returns no rows, rather than an invalid XML document. Note that IS DOCUMENT is false for the empty XML returned, so it can be trapped in a query. But if query_to_xml() is nested somehow (like the above) this is really complicated to apply and I find it a bit surprising that query_to_xml() returns invalid XML at all Tested with 10.2 on Windows 10 Thomas