Re: xpath insert unexpected newlines

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: "Voillequin, Jean-Marc" <Jean-Marc(dot)Voillequin(at)moodys(dot)com>
Cc: pgsql-bugs(at)lists(dot)postgresql(dot)org
Subject: Re: xpath insert unexpected newlines
Date: 2019-04-19 14:21:23
Message-ID: 8718.1555683683@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-bugs pgsql-sql

[ redirecting to pgsql-bugs ]

"Voillequin, Jean-Marc" <Jean-Marc(dot)Voillequin(at)moodys(dot)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

In response to

Responses

Browse pgsql-bugs by date

  From Date Subject
Next Message Nick Anderson 2019-04-19 16:48:29 RE: Re: Re: BUG #15769: The database cluster intialisation failed.
Previous Message PG Bug reporting form 2019-04-19 13:29:30 BUG #15773: Cannot Open a sql file directly - have to go to pgadmin, query tool and then choose open

Browse pgsql-sql by date

  From Date Subject
Next Message Tom Lane 2019-04-22 19:48:38 Re: xpath insert unexpected newlines
Previous Message Voillequin, Jean-Marc 2019-04-19 13:04:37 xpath insert unexpected newlines