Skip to content

Commit 1646586

Browse files
committed
Attached is a patch that adds the function xml_encode_special_chars to
the xml2 contrib module. It's against 8.0beta4. It's intended for commit. Markus Bertheau <twanger@bluetwanger.de>
1 parent 7af770d commit 1646586

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

contrib/xml2/pgxml.sql.in

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
CREATE OR REPLACE FUNCTION xml_valid(text) RETURNS bool
44
AS 'MODULE_PATHNAME' LANGUAGE 'c' WITH (isStrict);
55

6+
CREATE OR REPLACE FUNCTION xml_encode_special_chars(text) RETURNS text
7+
AS 'MODULE_PATHNAME' LANGUAGE 'c' WITH (isStrict);
8+
69
CREATE OR REPLACE FUNCTION xpath_string(text,text) RETURNS text
710
AS 'MODULE_PATHNAME' LANGUAGE 'c' WITH (isStrict);
811

contrib/xml2/xpath.c

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ static xmlXPathObjectPtr pgxml_xpath(text *document, xmlChar * xpath);
4040

4141

4242
Datum xml_valid(PG_FUNCTION_ARGS);
43+
Datum xml_encode_special_chars(PG_FUNCTION_ARGS);
4344
Datum xpath_nodeset(PG_FUNCTION_ARGS);
4445
Datum xpath_string(PG_FUNCTION_ARGS);
4546
Datum xpath_number(PG_FUNCTION_ARGS);
@@ -186,6 +187,34 @@ xml_valid(PG_FUNCTION_ARGS)
186187
}
187188

188189

190+
/* Encodes special characters (<, >, &, " and \r) as XML entities */
191+
192+
PG_FUNCTION_INFO_V1(xml_encode_special_chars);
193+
194+
Datum
195+
xml_encode_special_chars(PG_FUNCTION_ARGS)
196+
{
197+
text *tin = PG_GETARG_TEXT_P(0);
198+
text *tout;
199+
int32 ressize;
200+
xmlChar *ts, *tt;
201+
202+
ts = pgxml_texttoxmlchar(tin);
203+
204+
tt = xmlEncodeSpecialChars(NULL, ts);
205+
206+
pfree(ts);
207+
208+
ressize = strlen(tt);
209+
tout = (text *) palloc(ressize + VARHDRSZ);
210+
memcpy(VARDATA(tout), tt, ressize);
211+
VARATT_SIZEP(tout) = ressize + VARHDRSZ;
212+
213+
xmlFree(tt);
214+
215+
PG_RETURN_TEXT_P(tout);
216+
}
217+
189218
static xmlChar
190219
*
191220
pgxmlNodeSetToText(xmlNodeSetPtr nodeset,

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy