Re: xpath insert unexpected newlines - Mailing list pgsql-bugs

From Tom Lane
Subject Re: xpath insert unexpected newlines
Date
Msg-id 8718.1555683683@sss.pgh.pa.us
Whole thread Raw
Responses Re: xpath insert unexpected newlines  (Tom Lane <tgl@sss.pgh.pa.us>)
List pgsql-bugs
[ 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>numberone</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:piecexmlns:local=\"http://127.0.0.1\" id=\"2\"/>"} 
 (1 row)


Thoughts?

            regards, tom lane



pgsql-bugs by date:

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