diff options
author | Bruce Momjian | 2004-12-02 22:21:12 +0000 |
---|---|---|
committer | Bruce Momjian | 2004-12-02 22:21:12 +0000 |
commit | 4e26409abd2feb09fa1f28f3d5eab2ee8a0e16d4 (patch) | |
tree | fadcde44de78db012243283b5c45d3169b388741 | |
parent | ba5890213b1fbf687209b7d8bcec924d648421b3 (diff) |
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 <[email protected]>
-rw-r--r-- | contrib/xml2/pgxml.sql.in | 3 | ||||
-rw-r--r-- | contrib/xml2/xpath.c | 29 |
2 files changed, 32 insertions, 0 deletions
diff --git a/contrib/xml2/pgxml.sql.in b/contrib/xml2/pgxml.sql.in index 9951d6c046..f9d8bd0e31 100644 --- a/contrib/xml2/pgxml.sql.in +++ b/contrib/xml2/pgxml.sql.in @@ -3,6 +3,9 @@ CREATE OR REPLACE FUNCTION xml_valid(text) RETURNS bool AS 'MODULE_PATHNAME' LANGUAGE 'c' WITH (isStrict); +CREATE OR REPLACE FUNCTION xml_encode_special_chars(text) RETURNS text + AS 'MODULE_PATHNAME' LANGUAGE 'c' WITH (isStrict); + CREATE OR REPLACE FUNCTION xpath_string(text,text) RETURNS text AS 'MODULE_PATHNAME' LANGUAGE 'c' WITH (isStrict); diff --git a/contrib/xml2/xpath.c b/contrib/xml2/xpath.c index ca95d41c22..d3ac76b9ef 100644 --- a/contrib/xml2/xpath.c +++ b/contrib/xml2/xpath.c @@ -40,6 +40,7 @@ static xmlXPathObjectPtr pgxml_xpath(text *document, xmlChar * xpath); Datum xml_valid(PG_FUNCTION_ARGS); +Datum xml_encode_special_chars(PG_FUNCTION_ARGS); Datum xpath_nodeset(PG_FUNCTION_ARGS); Datum xpath_string(PG_FUNCTION_ARGS); Datum xpath_number(PG_FUNCTION_ARGS); @@ -186,6 +187,34 @@ xml_valid(PG_FUNCTION_ARGS) } +/* Encodes special characters (<, >, &, " and \r) as XML entities */ + +PG_FUNCTION_INFO_V1(xml_encode_special_chars); + +Datum +xml_encode_special_chars(PG_FUNCTION_ARGS) +{ + text *tin = PG_GETARG_TEXT_P(0); + text *tout; + int32 ressize; + xmlChar *ts, *tt; + + ts = pgxml_texttoxmlchar(tin); + + tt = xmlEncodeSpecialChars(NULL, ts); + + pfree(ts); + + ressize = strlen(tt); + tout = (text *) palloc(ressize + VARHDRSZ); + memcpy(VARDATA(tout), tt, ressize); + VARATT_SIZEP(tout) = ressize + VARHDRSZ; + + xmlFree(tt); + + PG_RETURN_TEXT_P(tout); +} + static xmlChar * pgxmlNodeSetToText(xmlNodeSetPtr nodeset, |