From 526fe0d79914b2dfcfd79effd1ab26ff62469248 Mon Sep 17 00:00:00 2001 From: Daniel Gustafsson Date: Mon, 6 Nov 2023 09:38:29 +0100 Subject: Add XMLText function (SQL/XML X038) This function implements the standard XMLTest function, which converts text into xml text nodes. It uses the libxml2 function xmlEncodeSpecialChars to escape predefined entities (&"<>), so that those do not cause any conflict when concatenating the text node output with existing xml documents. This also adds a note in features.sgml about not supporting XML(SEQUENCE). The SQL specification defines a RETURNING clause to a set of XML functions, where RETURNING CONTENT or RETURNING SEQUENCE can be defined. Since PostgreSQL doesn't support XML(SEQUENCE) all of these functions operate with an implicit RETURNING CONTENT. Author: Jim Jones Reviewed-by: Vik Fearing Discussion: https://postgr.es/m/86617a66-ec95-581f-8d54-08059cca8885@uni-muenster.de --- src/backend/utils/adt/xml.c | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) (limited to 'src/backend/utils/adt/xml.c') diff --git a/src/backend/utils/adt/xml.c b/src/backend/utils/adt/xml.c index 2300c7ebf34..c401e7b8219 100644 --- a/src/backend/utils/adt/xml.c +++ b/src/backend/utils/adt/xml.c @@ -47,6 +47,7 @@ #ifdef USE_LIBXML #include +#include #include #include #include @@ -513,6 +514,27 @@ xmlcomment(PG_FUNCTION_ARGS) } +Datum +xmltext(PG_FUNCTION_ARGS) +{ +#ifdef USE_LIBXML + text *arg = PG_GETARG_TEXT_PP(0); + text *result; + xmlChar *xmlbuf = NULL; + + xmlbuf = xmlEncodeSpecialChars(NULL, xml_text2xmlChar(arg)); + + Assert(xmlbuf); + + result = cstring_to_text_with_len((const char *) xmlbuf, xmlStrlen(xmlbuf)); + xmlFree(xmlbuf); + PG_RETURN_XML_P(result); +#else + NO_XML_SUPPORT(); + return 0; +#endif /* not USE_LIBXML */ +} + /* * TODO: xmlconcat needs to merge the notations and unparsed entities -- cgit v1.2.3