From: Tom Lane <tgl@sss.pgh.pa.us>
To: Voillequin, Jean-Marc <Jean-Marc.Voillequin@moodys.com>
Cc: pgsql-bugs@lists.postgresql.org
Subject: Re: xpath insert unexpected newlines
Date: Fri, 19 Apr 2019 10:21:23 -0400
Message-ID: <8718.1555683683@sss.pgh.pa.us> (raw)
In-Reply-To: <1EC8157EB499BF459A516ADCF135ADCE3A23A9CA@LON-WGMSX712.ad.moodys.net>
References: <1EC8157EB499BF459A516ADCF135ADCE3A23A9CA@LON-WGMSX712.ad.moodys.net>
[ redirecting to pgsql-bugs ]
"Voillequin, Jean-Marc" <Jean-Marc.Voillequin@moodys.com> writes:
> It seems that xpath function add unexpected newlines in the xml elements it returns as array:
> postgres=> select ((xpath('/*',xml('<root><a/><b/><c/></root>')))[1])::text;
> xpath
> ---------
> <root> +
> <a/> +
> <b/> +
> <c/> +
> </root>
> (1 row)
> A workaround is to have at least one element with a value:
> postgres=> select ((xpath('/*',xml('<root><a/><b/><c/>one value</root>')))[1])::text;
> xpath
> ------------------------------------
> <root><a/><b/><c/>one value</root>
> (1 row)
Wow, that's bizarre. It seems that this behavior is down to xmlNodeDump,
which is what we use to produce xpath's output. I'm not sure why it
chooses to pretty-print one case and not the other, but I'd be inclined
to say that for PG's purposes we don't want any pretty-printing.
I tried this:
inline patch 1: src/backend/utils/adt/xml.c (+1/-1 lines) download inline-1.patchdiff --git a/src/backend/utils/adt/xml.c b/src/backend/utils/adt/xml.cindex dae7d58..48b8034 100644--- a/src/backend/utils/adt/xml.c+++ b/src/backend/utils/adt/xml.c@@ -3857,7 +3857,7 @@ xml_xmlnodetoxmltype(xmlNodePtr cur, PgXmlErrorContext *xmlerrcxt)
nodefree = (cur_copy->type == XML_DOCUMENT_NODE) ?
(void (*) (xmlNodePtr)) xmlFreeDoc : xmlFreeNode;
- bytes = xmlNodeDump(buf, NULL, cur_copy, 0, 1);+ bytes = xmlNodeDump(buf, NULL, cur_copy, 0, 0);
if (bytes == -1 || xmlerrcxt->err_occurred)
xml_ereport(xmlerrcxt, ERROR, ERRCODE_OUT_OF_MEMORY,
"could not dump node");
and that makes the discrepancy go away (both results are printed without
any added whitespace). There is one change in the regression test
outputs, which is an xpath query that's been affected by this very
same issue:
SELECT xpath('//loc:piece', '<local:data xmlns:local="http://127.0.0.1"; xmlns="http://127.0.0.2"><local:piece id="1"><internal>number one</internal><internal2/></local:piece><local:piece id="2" /></local:data>', ARRAY[ARRAY['loc', 'http://127.0.0.1';]]);
- xpath
---------------------------------------------------------------------------------------
- {"<local:piece xmlns:local=\"http://127.0.0.1\" xmlns=\"http://127.0.0.2\" id=\"1\">+
- <internal>number one</internal> +
- <internal2/> +
- </local:piece>","<local:piece xmlns:local=\"http://127.0.0.1\" id=\"2\"/>"}
+ xpath
+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
+ {"<local:piece xmlns:local=\"http://127.0.0.1\" xmlns=\"http://127.0.0.2\" id=\"1\"><internal>number one</internal><internal2/></local:piece>","<local:piece xmlns:local=\"http://127.0.0.1\" id=\"2\"/>"}
(1 row)
Thoughts?
regards, tom lane
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-bugs@postgresql.org
Cc: tgl@sss.pgh.pa.us, Jean-Marc.Voillequin@moodys.com
Subject: Re: xpath insert unexpected newlines
In-Reply-To: <8718.1555683683@sss.pgh.pa.us>
References: <1EC8157EB499BF459A516ADCF135ADCE3A23A9CA@LON-WGMSX712.ad.moodys.net> <8718.1555683683@sss.pgh.pa.us>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
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