agora inbox for pgsql-sql@postgresql.org
help / color / mirror / Atom feedFrom: 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:
diff --git a/src/backend/utils/adt/xml.c b/src/backend/utils/adt/xml.c
index 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
view thread (3+ messages) latest in thread
Message-ID: <8718.1555683683@sss.pgh.pa.us>
Permalink: ../8718.1555683683@sss.pgh.pa.us/
Also on: postgresql.org/message-id/8718.1555683683@sss.pgh.pa.us
reply
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 using the --to and --cc options:
reply via email
To: pgsql-sql@postgresql.org
Cc: tgl@sss.pgh.pa.us, Jean-Marc.Voillequin@moodys.com, pgsql-bugs@lists.postgresql.org
Subject: Re: xpath insert unexpected newlines
In-Reply-To: <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 agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox