PostgreSQL Source Code git master
Loading...
Searching...
No Matches
xslt_proc.c File Reference
#include "postgres.h"
#include "fmgr.h"
#include "utils/builtins.h"
#include "utils/xml.h"
#include "varatt.h"
Include dependency graph for xslt_proc.c:

Go to the source code of this file.

Functions

 PG_FUNCTION_INFO_V1 (xslt_process)
 
Datum xslt_process (PG_FUNCTION_ARGS)
 

Function Documentation

◆ PG_FUNCTION_INFO_V1()

PG_FUNCTION_INFO_V1 ( xslt_process  )

◆ xslt_process()

Datum xslt_process ( PG_FUNCTION_ARGS  )

Definition at line 46 of file xslt_proc.c.

47{
48#ifdef USE_LIBXSLT
49
52 text *volatile result = NULL;
54 const char **params;
57 volatile xmlDocPtr doctree = NULL;
58 volatile xmlDocPtr restree = NULL;
61 volatile int resstat = -1;
62 xmlChar *volatile resstr = NULL;
63
64 if (fcinfo->nargs == 3)
65 {
67 params = parse_params(paramstr);
68 }
69 else
70 {
71 /* No parameters */
72 params = palloc_object(const char *);
73 params[0] = NULL;
74 }
75
76 /* Setup parser */
78
79 PG_TRY();
80 {
83 int reslen = 0;
84
85 /* Parse document */
86 doctree = xmlReadMemory((char *) VARDATA_ANY(doct),
89
90 if (doctree == NULL || pg_xml_error_occurred(xmlerrcxt))
92 "error parsing XML document");
93
94 /* Same for stylesheet */
98
101 "error parsing stylesheet as XML document");
102
103 /* After this call we need not free ssdoc separately */
105
108 "failed to parse stylesheet");
109
111
112 xslt_sec_prefs_error = false;
115
117 xsltSecurityForbid) != 0)
120 xsltSecurityForbid) != 0)
123 xsltSecurityForbid) != 0)
126 xsltSecurityForbid) != 0)
129 xsltSecurityForbid) != 0)
133
136 (errmsg("could not set libxslt security preferences")));
137
138 restree = xsltApplyStylesheetUser(stylesheet, doctree, params,
140
143 "failed to apply stylesheet");
144
147
148 if (resstat >= 0)
149 result = cstring_to_text_with_len((char *) resstr, reslen);
150 }
151 PG_CATCH();
152 {
153 if (restree != NULL)
155 if (xslt_ctxt != NULL)
157 if (xslt_sec_prefs != NULL)
159 if (stylesheet != NULL)
161 if (doctree != NULL)
162 xmlFreeDoc(doctree);
163 if (resstr != NULL)
166
167 pg_xml_done(xmlerrcxt, true);
168
169 PG_RE_THROW();
170 }
171 PG_END_TRY();
172
177 xmlFreeDoc(doctree);
179
180 if (resstr)
182
183 pg_xml_done(xmlerrcxt, false);
184
185 /* XXX this is pretty dubious, really ought to throw error instead */
186 if (resstat < 0)
188
189 PG_RETURN_TEXT_P(result);
190#else /* !USE_LIBXSLT */
191
194 errmsg("xslt_process() is not available without libxslt")));
196#endif /* USE_LIBXSLT */
197}
int errcode(int sqlerrcode)
Definition elog.c:863
int errmsg(const char *fmt,...)
Definition elog.c:1080
#define PG_RE_THROW()
Definition elog.h:405
#define PG_TRY(...)
Definition elog.h:372
#define PG_END_TRY(...)
Definition elog.h:397
#define ERROR
Definition elog.h:39
#define PG_CATCH(...)
Definition elog.h:382
#define ereport(elevel,...)
Definition elog.h:150
#define palloc_object(type)
Definition fe_memutils.h:74
#define PG_GETARG_TEXT_PP(n)
Definition fmgr.h:310
#define PG_RETURN_NULL()
Definition fmgr.h:346
#define PG_RETURN_TEXT_P(x)
Definition fmgr.h:374
static int fb(int x)
Definition c.h:706
static Size VARSIZE_ANY_EXHDR(const void *PTR)
Definition varatt.h:472
static char * VARDATA_ANY(const void *PTR)
Definition varatt.h:486
text * cstring_to_text_with_len(const char *s, int len)
Definition varlena.c:193
struct PgXmlErrorContext PgXmlErrorContext
Definition xml.h:48
void xml_ereport(PgXmlErrorContext *errcxt, int level, int sqlcode, const char *msg)
bool pg_xml_error_occurred(PgXmlErrorContext *errcxt)
void pg_xml_done(PgXmlErrorContext *errcxt, bool isError)
@ PG_XML_STRICTNESS_LEGACY
Definition xml.h:41
PgXmlErrorContext * pgxml_parser_init(PgXmlStrictness strictness)
Definition xpath.c:67

References cstring_to_text_with_len(), ereport, errcode(), errmsg(), ERROR, fb(), palloc_object, PG_CATCH, PG_END_TRY, PG_GETARG_TEXT_PP, PG_RE_THROW, PG_RETURN_NULL, PG_RETURN_TEXT_P, PG_TRY, pg_xml_done(), pg_xml_error_occurred(), PG_XML_STRICTNESS_LEGACY, pgxml_parser_init(), VARDATA_ANY(), VARSIZE_ANY_EXHDR(), and xml_ereport().