From: Tom Lane Date: Tue, 10 Sep 2024 20:20:31 +0000 (-0400) Subject: Fix some whitespace issues in XMLSERIALIZE(... INDENT). X-Git-Tag: REL_16_5~100 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=06c285018a81ce4364e370d276be796a632115f8;p=thirdparty%2Fpostgresql.git Fix some whitespace issues in XMLSERIALIZE(... INDENT). We must drop whitespace while parsing the input, else libxml2 will include "blank" nodes that interfere with the desired indentation behavior. The end result is that we didn't indent nodes separated by whitespace. Also, it seems that libxml2 may add a trailing newline when working in DOCUMENT mode. This is semantically insignificant, so strip it. This is in the gray area between being a bug fix and a definition change. However, the INDENT option is still pretty new (since v16), so I think we can get away with changing this in stable branches. Hence, back-patch to v16. Jim Jones Discussion: https://postgr.es/m/872865a8-548b-48e1-bfcd-4e38e672c1e4@uni-muenster.de --- diff --git a/src/backend/utils/adt/xml.c b/src/backend/utils/adt/xml.c index 0255349aa43..68bbf86cc46 100644 --- a/src/backend/utils/adt/xml.c +++ b/src/backend/utils/adt/xml.c @@ -656,8 +656,14 @@ xmltotext_with_options(xmltype *data, XmlOptionType xmloption_arg, bool indent) } #ifdef USE_LIBXML - /* Parse the input according to the xmloption */ - doc = xml_parse(data, xmloption_arg, true, GetDatabaseEncoding(), + + /* + * Parse the input according to the xmloption. + * + * preserve_whitespace is set to false in case we are indenting, otherwise + * libxml2 will fail to indent elements that have whitespace between them. + */ + doc = xml_parse(data, xmloption_arg, !indent, GetDatabaseEncoding(), &parsed_xmloptiontype, &content_nodes, (Node *) &escontext); if (doc == NULL || escontext.error_occurred) @@ -781,7 +787,22 @@ xmltotext_with_options(xmltype *data, XmlOptionType xmloption_arg, bool indent) "could not close xmlSaveCtxtPtr"); } - result = (text *) xmlBuffer_to_xmltype(buf); + /* + * xmlDocContentDumpOutput may add a trailing newline, so remove that. + */ + if (xmloption_arg == XMLOPTION_DOCUMENT) + { + const char *str = (const char *) xmlBufferContent(buf); + int len = xmlBufferLength(buf); + + while (len > 0 && (str[len - 1] == '\n' || + str[len - 1] == '\r')) + len--; + + result = cstring_to_text_with_len(str, len); + } + else + result = (text *) xmlBuffer_to_xmltype(buf); } PG_CATCH(); { diff --git a/src/test/regress/expected/xml.out b/src/test/regress/expected/xml.out index 15d99185794..894ee6bd2b7 100644 --- a/src/test/regress/expected/xml.out +++ b/src/test/regress/expected/xml.out @@ -485,8 +485,7 @@ SELECT xmlserialize(DOCUMENT '42' AS text + 42+ + - + - + (1 row) SELECT xmlserialize(CONTENT '42' AS text INDENT); @@ -546,8 +545,7 @@ SELECT xmlserialize(DOCUMENT '42text node< 42 + text node73+ + - + - + (1 row) SELECT xmlserialize(CONTENT '42text node73' AS text INDENT); @@ -601,8 +599,7 @@ SELECT xmlserialize(DOCUMENT ' + 73 + + - + - + (1 row) SELECT xmlserialize(CONTENT '73' AS text INDENT); @@ -620,8 +617,7 @@ SELECT xmlserialize(DOCUMENT '' AS text INDENT); xmlserialize -------------- + - + - + (1 row) SELECT xmlserialize(CONTENT '' AS text INDENT); @@ -638,8 +634,7 @@ SELECT xmlserialize(DOCUMENT '' AS text INDENT); -------------- + + - + - + (1 row) SELECT xmlserialize(CONTENT '' AS text INDENT); @@ -663,6 +658,24 @@ SELECT xmlserialize(CONTENT '42' AS text t (1 row) +-- indent xml strings containing blank nodes +SELECT xmlserialize(DOCUMENT ' ' AS text INDENT); + xmlserialize +-------------- + + + + + +(1 row) + +SELECT xmlserialize(CONTENT 'text node ' AS text INDENT); + xmlserialize +-------------- + text node + + + + + + +(1 row) + SELECT xml 'bar' IS DOCUMENT; ?column? ---------- diff --git a/src/test/regress/expected/xml_1.out b/src/test/regress/expected/xml_1.out index 63b779470ff..7e9611f1d38 100644 --- a/src/test/regress/expected/xml_1.out +++ b/src/test/regress/expected/xml_1.out @@ -443,6 +443,17 @@ ERROR: unsupported XML feature LINE 1: SELECT xmlserialize(CONTENT '42<... ^ DETAIL: This functionality requires the server to be built with libxml support. +-- indent xml strings containing blank nodes +SELECT xmlserialize(DOCUMENT ' ' AS text INDENT); +ERROR: unsupported XML feature +LINE 1: SELECT xmlserialize(DOCUMENT ' '... + ^ +DETAIL: This functionality requires the server to be built with libxml support. +SELECT xmlserialize(CONTENT 'text node ' AS text INDENT); +ERROR: unsupported XML feature +LINE 1: SELECT xmlserialize(CONTENT 'text node ... + ^ +DETAIL: This functionality requires the server to be built with libxml support. SELECT xml 'bar' IS DOCUMENT; ERROR: unsupported XML feature LINE 1: SELECT xml 'bar' IS DOCUMENT; diff --git a/src/test/regress/expected/xml_2.out b/src/test/regress/expected/xml_2.out index 8894f7b4a84..7d5c961e240 100644 --- a/src/test/regress/expected/xml_2.out +++ b/src/test/regress/expected/xml_2.out @@ -471,8 +471,7 @@ SELECT xmlserialize(DOCUMENT '42' AS text + 42+ + - + - + (1 row) SELECT xmlserialize(CONTENT '42' AS text INDENT); @@ -532,8 +531,7 @@ SELECT xmlserialize(DOCUMENT '42text node< 42 + text node73+ + - + - + (1 row) SELECT xmlserialize(CONTENT '42text node73' AS text INDENT); @@ -587,8 +585,7 @@ SELECT xmlserialize(DOCUMENT ' + 73 + + - + - + (1 row) SELECT xmlserialize(CONTENT '73' AS text INDENT); @@ -606,8 +603,7 @@ SELECT xmlserialize(DOCUMENT '' AS text INDENT); xmlserialize -------------- + - + - + (1 row) SELECT xmlserialize(CONTENT '' AS text INDENT); @@ -624,8 +620,7 @@ SELECT xmlserialize(DOCUMENT '' AS text INDENT); -------------- + + - + - + (1 row) SELECT xmlserialize(CONTENT '' AS text INDENT); @@ -649,6 +644,24 @@ SELECT xmlserialize(CONTENT '42' AS text t (1 row) +-- indent xml strings containing blank nodes +SELECT xmlserialize(DOCUMENT ' ' AS text INDENT); + xmlserialize +-------------- + + + + + +(1 row) + +SELECT xmlserialize(CONTENT 'text node ' AS text INDENT); + xmlserialize +-------------- + text node + + + + + + +(1 row) + SELECT xml 'bar' IS DOCUMENT; ?column? ---------- diff --git a/src/test/regress/sql/xml.sql b/src/test/regress/sql/xml.sql index a591eea2e5d..0b07075414e 100644 --- a/src/test/regress/sql/xml.sql +++ b/src/test/regress/sql/xml.sql @@ -168,6 +168,9 @@ SELECT xmlserialize(CONTENT '' AS text INDENT); -- 'no indent' = not using 'no indent' SELECT xmlserialize(DOCUMENT '42' AS text) = xmlserialize(DOCUMENT '42' AS text NO INDENT); SELECT xmlserialize(CONTENT '42' AS text) = xmlserialize(CONTENT '42' AS text NO INDENT); +-- indent xml strings containing blank nodes +SELECT xmlserialize(DOCUMENT ' ' AS text INDENT); +SELECT xmlserialize(CONTENT 'text node ' AS text INDENT); SELECT xml 'bar' IS DOCUMENT; SELECT xml 'barfoo' IS DOCUMENT;