pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
query_to_xml() returns invalid XML when query returns no rows
4+ messages / 2 participants
[nested] [flat]

* query_to_xml() returns invalid XML when query returns no rows
@ 2018-03-12 12:18 Thomas Kellerer <spam_eater@gmx.net>
  2018-03-12 22:31 ` Re: query_to_xml() returns invalid XML when query returns no rows Peter Eisentraut <peter.eisentraut@2ndquadrant.com>
  0 siblings, 1 reply; 4+ messages in thread

From: Thomas Kellerer @ 2018-03-12 12:18 UTC (permalink / raw)
  To: pgsql-general

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








^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: query_to_xml() returns invalid XML when query returns no rows
  2018-03-12 12:18 query_to_xml() returns invalid XML when query returns no rows Thomas Kellerer <spam_eater@gmx.net>
@ 2018-03-12 22:31 ` Peter Eisentraut <peter.eisentraut@2ndquadrant.com>
  2018-03-13 19:21   ` Re: query_to_xml() returns invalid XML when query returns no rows Thomas Kellerer <spam_eater@gmx.net>
  0 siblings, 1 reply; 4+ messages in thread

From: Peter Eisentraut @ 2018-03-12 22:31 UTC (permalink / raw)
  To: Thomas Kellerer <spam_eater@gmx.net>; pgsql-general

On 3/12/18 08:18, Thomas Kellerer wrote:
> 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:

That's because you have the tableforest argument set to true.  If you
want a proper XML document, then you should write

    select query_to_xml('select 42 where false', false, false, '');

-- 
Peter Eisentraut              http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services




^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: query_to_xml() returns invalid XML when query returns no rows
  2018-03-12 12:18 query_to_xml() returns invalid XML when query returns no rows Thomas Kellerer <spam_eater@gmx.net>
  2018-03-12 22:31 ` Re: query_to_xml() returns invalid XML when query returns no rows Peter Eisentraut <peter.eisentraut@2ndquadrant.com>
@ 2018-03-13 19:21   ` Thomas Kellerer <spam_eater@gmx.net>
  2018-03-13 22:28     ` Re: query_to_xml() returns invalid XML when query returns no rows Peter Eisentraut <peter.eisentraut@2ndquadrant.com>
  0 siblings, 1 reply; 4+ messages in thread

From: Thomas Kellerer @ 2018-03-13 19:21 UTC (permalink / raw)
  To: pgsql-general

Peter Eisentraut schrieb am 12.03.2018 um 23:31:
>> 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:
> 
> That's because you have the tableforest argument set to true.  If you
> want a proper XML document, then you should write
> 
>      select query_to_xml('select 42 where false', false, false, '');
> 

Hmm, that indeed works.
I didn't want the extra level introduced by the <table> tag, that's why I used tableforest = true.
But that's easier to deal with than the check for an invalid document

I still think it's incorrect to return an empty (=invalid) XML instead of a NULL value though.

Regards
Thomas
  






^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: query_to_xml() returns invalid XML when query returns no rows
  2018-03-12 12:18 query_to_xml() returns invalid XML when query returns no rows Thomas Kellerer <spam_eater@gmx.net>
  2018-03-12 22:31 ` Re: query_to_xml() returns invalid XML when query returns no rows Peter Eisentraut <peter.eisentraut@2ndquadrant.com>
  2018-03-13 19:21   ` Re: query_to_xml() returns invalid XML when query returns no rows Thomas Kellerer <spam_eater@gmx.net>
@ 2018-03-13 22:28     ` Peter Eisentraut <peter.eisentraut@2ndquadrant.com>
  0 siblings, 0 replies; 4+ messages in thread

From: Peter Eisentraut @ 2018-03-13 22:28 UTC (permalink / raw)
  To: Thomas Kellerer <spam_eater@gmx.net>; pgsql-general

On 3/13/18 15:21, Thomas Kellerer wrote:
> I still think it's incorrect to return an empty (=invalid) XML instead of a NULL value though.

This behavior is specified in the SQL standard.  While an empty string
is not a valid XML "document", it is valid as XML "content".

-- 
Peter Eisentraut              http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services




^ permalink  raw  reply  [nested|flat] 4+ messages in thread


end of thread, other threads:[~2018-03-13 22:28 UTC | newest]

Thread overview: 4+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2018-03-12 12:18 query_to_xml() returns invalid XML when query returns no rows Thomas Kellerer <spam_eater@gmx.net>
2018-03-12 22:31 ` Peter Eisentraut <peter.eisentraut@2ndquadrant.com>
2018-03-13 19:21   ` Thomas Kellerer <spam_eater@gmx.net>
2018-03-13 22:28     ` Peter Eisentraut <peter.eisentraut@2ndquadrant.com>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox