My inclination would be, if we're just calling to a long-standardized library routine, to just accept its output as is. If a program is saving the output to a text file, that would be the expected behaviour. If not, then we need to document that the output of our function is the output of the library function, minus the trailing newline.
After some digging on the matter, I'd also tend to leave the output as is, but I also do understand the other arguments - specially the consistency with jsonb_pretty().
If we agree to remove it, the change wouldn't be substantial :) I guess we could just pchomp it in the end of the function, as suggested by Tom. Attached a draft patch.
Best, Jim
diff --git a/src/backend/utils/adt/xml.c b/src/backend/utils/adt/xml.c index 15adbd6a01..b282330ffe 100644 --- a/src/backend/utils/adt/xml.c +++ b/src/backend/utils/adt/xml.c @@ -771,7 +771,18 @@ xmltotext_with_options(xmltype *data, XmlOptionType xmloption_arg, bool indent) "could not close xmlSaveCtxtPtr"); } - result = (text *) xmlBuffer_to_xmltype(buf); + /* + * This is necessary to remove the trailing newline created + * by xmlSaveDoc - it only affects DOCUMENT xml strings. + * The fragments of CONTENT strings are stored into the + * xmlBufferPtr using xmlSaveTree, which does not add a + * trailing newline. + */ + if(xmloption_arg != XMLOPTION_DOCUMENT) + result = (text *) xmlBuffer_to_xmltype(buf); + else + result = cstring_to_text_with_len(pchomp((const char *) xmlBufferContent(buf)), + xmlBufferLength(buf)); } PG_CATCH(); { diff --git a/src/test/regress/expected/xml.out b/src/test/regress/expected/xml.out index 398345ca67..b689f86fe6 100644 --- a/src/test/regress/expected/xml.out +++ b/src/test/regress/expected/xml.out @@ -494,8 +494,7 @@ SELECT xmlserialize(DOCUMENT '<foo><bar><val x="y">42</val></bar></foo>' AS text <bar> + <val x="y">42</val>+ </bar> + - </foo> + - + </foo> (1 row) SELECT xmlserialize(CONTENT '<foo><bar><val x="y">42</val></bar></foo>' AS text INDENT); @@ -555,8 +554,7 @@ SELECT xmlserialize(DOCUMENT '<foo><bar><val x="y">42</val><val x="y">text node< <val x="y">42</val> + <val x="y">text node<val>73</val></val>+ </bar> + - </foo> + - + </foo> (1 row) SELECT xmlserialize(CONTENT '<foo><bar><val x="y">42</val><val x="y">text node<val>73</val></val></bar></foo>' AS text INDENT); @@ -610,8 +608,7 @@ SELECT xmlserialize(DOCUMENT '<?xml version="1.0" encoding="UTF-8"?><foo><bar><v <bar> + <val>73</val> + </bar> + - </foo> + - + </foo> (1 row) SELECT xmlserialize(CONTENT '<?xml version="1.0" encoding="UTF-8"?><foo><bar><val>73</val></bar></foo>' AS text INDENT); @@ -629,8 +626,7 @@ SELECT xmlserialize(DOCUMENT '<!DOCTYPE a><a/>' AS text INDENT); xmlserialize -------------- <!DOCTYPE a>+ - <a/> + - + <a/> (1 row) SELECT xmlserialize(CONTENT '<!DOCTYPE a><a/>' AS text INDENT); @@ -647,8 +643,7 @@ SELECT xmlserialize(DOCUMENT '<foo><bar></bar></foo>' AS text INDENT); -------------- <foo> + <bar/> + - </foo> + - + </foo> (1 row) SELECT xmlserialize(CONTENT '<foo><bar></bar></foo>' AS text INDENT); diff --git a/src/test/regress/expected/xml_2.out b/src/test/regress/expected/xml_2.out index 43c2558352..a2eeff8369 100644 --- a/src/test/regress/expected/xml_2.out +++ b/src/test/regress/expected/xml_2.out @@ -474,8 +474,7 @@ SELECT xmlserialize(DOCUMENT '<foo><bar><val x="y">42</val></bar></foo>' AS text <bar> + <val x="y">42</val>+ </bar> + - </foo> + - + </foo> (1 row) SELECT xmlserialize(CONTENT '<foo><bar><val x="y">42</val></bar></foo>' AS text INDENT); @@ -535,8 +534,7 @@ SELECT xmlserialize(DOCUMENT '<foo><bar><val x="y">42</val><val x="y">text node< <val x="y">42</val> + <val x="y">text node<val>73</val></val>+ </bar> + - </foo> + - + </foo> (1 row) SELECT xmlserialize(CONTENT '<foo><bar><val x="y">42</val><val x="y">text node<val>73</val></val></bar></foo>' AS text INDENT); @@ -590,8 +588,7 @@ SELECT xmlserialize(DOCUMENT '<?xml version="1.0" encoding="UTF-8"?><foo><bar><v <bar> + <val>73</val> + </bar> + - </foo> + - + </foo> (1 row) SELECT xmlserialize(CONTENT '<?xml version="1.0" encoding="UTF-8"?><foo><bar><val>73</val></bar></foo>' AS text INDENT); @@ -609,8 +606,7 @@ SELECT xmlserialize(DOCUMENT '<!DOCTYPE a><a/>' AS text INDENT); xmlserialize -------------- <!DOCTYPE a>+ - <a/> + - + <a/> (1 row) SELECT xmlserialize(CONTENT '<!DOCTYPE a><a/>' AS text INDENT); @@ -627,8 +623,7 @@ SELECT xmlserialize(DOCUMENT '<foo><bar></bar></foo>' AS text INDENT); -------------- <foo> + <bar/> + - </foo> + - + </foo> (1 row) SELECT xmlserialize(CONTENT '<foo><bar></bar></foo>' AS text INDENT);