21 #include <libxml/xpath.h>
22 #include <libxml/tree.h>
23 #include <libxml/xmlmemory.h>
27 #include <libxslt/xslt.h>
28 #include <libxslt/xsltInternals.h>
29 #include <libxslt/security.h>
30 #include <libxslt/transform.h>
31 #include <libxslt/xsltutils.h>
41 static const char **parse_params(
text *paramstr);
58 volatile xsltStylesheetPtr stylesheet = NULL;
59 volatile xmlDocPtr doctree = NULL;
60 volatile xmlDocPtr restree = NULL;
61 volatile xsltSecurityPrefsPtr xslt_sec_prefs = NULL;
62 volatile xsltTransformContextPtr xslt_ctxt = NULL;
63 volatile int resstat = -1;
64 xmlChar *resstr = NULL;
67 if (fcinfo->nargs == 3)
70 params = parse_params(paramstr);
75 params = (
const char **)
palloc(
sizeof(
char *));
85 bool xslt_sec_prefs_error;
94 "error parsing XML document");
103 "error parsing stylesheet as XML document");
106 stylesheet = xsltParseStylesheetDoc(ssdoc);
108 if (stylesheet == NULL)
110 "failed to parse stylesheet");
112 xslt_ctxt = xsltNewTransformContext(stylesheet, doctree);
114 xslt_sec_prefs_error =
false;
115 if ((xslt_sec_prefs = xsltNewSecurityPrefs()) == NULL)
116 xslt_sec_prefs_error =
true;
118 if (xsltSetSecurityPrefs(xslt_sec_prefs, XSLT_SECPREF_READ_FILE,
119 xsltSecurityForbid) != 0)
120 xslt_sec_prefs_error =
true;
121 if (xsltSetSecurityPrefs(xslt_sec_prefs, XSLT_SECPREF_WRITE_FILE,
122 xsltSecurityForbid) != 0)
123 xslt_sec_prefs_error =
true;
124 if (xsltSetSecurityPrefs(xslt_sec_prefs, XSLT_SECPREF_CREATE_DIRECTORY,
125 xsltSecurityForbid) != 0)
126 xslt_sec_prefs_error =
true;
127 if (xsltSetSecurityPrefs(xslt_sec_prefs, XSLT_SECPREF_READ_NETWORK,
128 xsltSecurityForbid) != 0)
129 xslt_sec_prefs_error =
true;
130 if (xsltSetSecurityPrefs(xslt_sec_prefs, XSLT_SECPREF_WRITE_NETWORK,
131 xsltSecurityForbid) != 0)
132 xslt_sec_prefs_error =
true;
133 if (xsltSetCtxtSecurityPrefs(xslt_sec_prefs, xslt_ctxt) != 0)
134 xslt_sec_prefs_error =
true;
136 if (xslt_sec_prefs_error)
138 (
errmsg(
"could not set libxslt security preferences")));
140 restree = xsltApplyStylesheetUser(stylesheet, doctree, params,
141 NULL, NULL, xslt_ctxt);
145 "failed to apply stylesheet");
147 resstat = xsltSaveResultToString(&resstr, &reslen, restree, stylesheet);
153 if (xslt_ctxt != NULL)
154 xsltFreeTransformContext(xslt_ctxt);
155 if (xslt_sec_prefs != NULL)
156 xsltFreeSecurityPrefs(xslt_sec_prefs);
157 if (stylesheet != NULL)
158 xsltFreeStylesheet(stylesheet);
161 xsltCleanupGlobals();
170 xsltFreeTransformContext(xslt_ctxt);
171 xsltFreeSecurityPrefs(xslt_sec_prefs);
172 xsltFreeStylesheet(stylesheet);
174 xsltCleanupGlobals();
191 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
192 errmsg(
"xslt_process() is not available without libxslt")));
200 parse_params(
text *paramstr)
213 params = (
const char **)
palloc((max_params + 1) *
sizeof(
char *));
220 if (nparams >= max_params)
223 params = (
const char **)
repalloc(params,
224 (max_params + 1) *
sizeof(
char *));
226 params[nparams++] = pos;
227 pos = strstr(pos, nvsep);
241 params[nparams++] = pos;
242 pos = strstr(pos, itsep);
253 params[nparams] = NULL;
int errcode(int sqlerrcode)
int errmsg(const char *fmt,...)
#define ereport(elevel,...)
#define PG_GETARG_TEXT_PP(n)
#define PG_RETURN_TEXT_P(x)
void * repalloc(void *pointer, Size size)
#define VARSIZE_ANY_EXHDR(PTR)
char * text_to_cstring(const text *t)
text * cstring_to_text_with_len(const char *s, int len)
struct PgXmlErrorContext PgXmlErrorContext
void xml_ereport(PgXmlErrorContext *errcxt, int level, int sqlcode, const char *msg)
void pg_xml_done(PgXmlErrorContext *errcxt, bool isError)
@ PG_XML_STRICTNESS_LEGACY
PgXmlErrorContext * pgxml_parser_init(PgXmlStrictness strictness)
PG_FUNCTION_INFO_V1(xslt_process)
Datum xslt_process(PG_FUNCTION_ARGS)