20 #include <libxml/xpath.h>
21 #include <libxml/tree.h>
22 #include <libxml/xmlmemory.h>
23 #include <libxml/xmlerror.h>
24 #include <libxml/parserInternals.h>
38 xmlXPathObjectPtr
res;
44 xmlChar *toptagname, xmlChar *septagname,
48 xmlChar *septag, xmlChar *plainsep);
95 tt = xmlEncodeSpecialChars(NULL, ts);
127 buf = xmlBufferCreate();
129 if ((toptagname != NULL) && (xmlStrlen(toptagname) > 0))
131 xmlBufferWriteChar(
buf,
"<");
132 xmlBufferWriteCHAR(
buf, toptagname);
133 xmlBufferWriteChar(
buf,
">");
137 for (
i = 0;
i < nodeset->nodeNr;
i++)
139 if (plainsep != NULL)
141 xmlBufferWriteCHAR(
buf,
142 xmlXPathCastNodeToString(nodeset->nodeTab[
i]));
145 if (
i < (nodeset->nodeNr) - 1)
146 xmlBufferWriteChar(
buf, (
char *) plainsep);
150 if ((septagname != NULL) && (xmlStrlen(septagname) > 0))
152 xmlBufferWriteChar(
buf,
"<");
153 xmlBufferWriteCHAR(
buf, septagname);
154 xmlBufferWriteChar(
buf,
">");
157 nodeset->nodeTab[
i]->doc,
161 if ((septagname != NULL) && (xmlStrlen(septagname) > 0))
163 xmlBufferWriteChar(
buf,
"</");
164 xmlBufferWriteCHAR(
buf, septagname);
165 xmlBufferWriteChar(
buf,
">");
171 if ((toptagname != NULL) && (xmlStrlen(toptagname) > 0))
173 xmlBufferWriteChar(
buf,
"</");
174 xmlBufferWriteCHAR(
buf, toptagname);
175 xmlBufferWriteChar(
buf,
">");
177 result = xmlStrdup(
buf->content);
209 xmlXPathObjectPtr
res;
241 xmlXPathObjectPtr
res;
270 xmlXPathObjectPtr
res;
282 memcpy((
char *)
xpath,
"string(", 7);
284 xpath[pathsize + 7] =
')';
285 xpath[pathsize + 8] =
'\0';
310 xmlXPathObjectPtr
res;
322 fRes = xmlXPathCastToNumber(
res);
326 if (xmlXPathIsNaN(fRes))
342 xmlXPathObjectPtr
res;
354 bRes = xmlXPathCastToBoolean(
res);
365 static xmlXPathObjectPtr
370 xmlXPathCompExprPtr comppath;
373 workspace->
ctxt = NULL;
374 workspace->
res = NULL;
383 if (workspace->
doctree != NULL)
385 workspace->
ctxt = xmlXPathNewContext(workspace->
doctree);
386 workspace->
ctxt->node = xmlDocGetRootElement(workspace->
doctree);
389 comppath = xmlXPathCtxtCompile(workspace->
ctxt,
xpath);
390 if (comppath == NULL)
392 "XPath Syntax Error");
395 workspace->
res = xmlXPathCompiledEval(comppath, workspace->
ctxt);
397 xmlXPathFreeCompExpr(comppath);
410 if (workspace->
res == NULL)
415 return workspace->
res;
423 xmlXPathFreeObject(workspace->
res);
424 workspace->
res = NULL;
426 xmlXPathFreeContext(workspace->
ctxt);
427 workspace->
ctxt = NULL;
429 xmlFreeDoc(workspace->
doctree);
454 xpresstr = xmlStrdup(
res->stringval);
459 xpresstr = xmlStrdup((
const xmlChar *)
"<unsupported/>");
499 const char *pathsep =
"|";
510 volatile xmlDocPtr doctree = NULL;
517 (
errcode(ERRCODE_SYNTAX_ERROR),
518 errmsg(
"xpath_table must have at least one output column")));
542 xpaths[numpaths++] = (xmlChar *) pos;
543 pos = strstr(pos, pathsep);
566 elog(
ERROR,
"xpath_table: SPI execution failed for query %s",
571 spi_tupdesc = tuptable->
tupdesc;
578 if (spi_tupdesc->
natts != 2)
581 errmsg(
"expression returning multiple columns is not valid in parameter list"),
582 errdetail(
"Expected two columns in SPI result, got %d.", spi_tupdesc->
natts)));
596 for (
i = 0;
i < proc;
i++)
600 xmlXPathContextPtr ctxt;
601 xmlXPathObjectPtr
res;
603 xmlXPathCompExprPtr comppath;
607 spi_tuple = tuptable->
vals[
i];
624 doctree = xmlReadMemory(xmldoc, strlen(xmldoc),
646 for (
j = 0;
j < numpaths;
j++)
648 ctxt = xmlXPathNewContext(doctree);
649 ctxt->node = xmlDocGetRootElement(doctree);
652 comppath = xmlXPathCtxtCompile(ctxt, xpaths[
j]);
653 if (comppath == NULL)
655 ERRCODE_EXTERNAL_ROUTINE_EXCEPTION,
656 "XPath Syntax Error");
659 res = xmlXPathCompiledEval(comppath, ctxt);
660 xmlXPathFreeCompExpr(comppath);
668 if (
res->nodesetval != NULL &&
669 rownr < res->nodesetval->nodeNr)
671 resstr = xmlXPathCastNodeToString(
res->nodesetval->nodeTab[rownr]);
680 resstr = xmlStrdup(
res->stringval);
685 resstr = xmlStrdup((
const xmlChar *)
"<unsupported/>");
692 values[
j + 1] = (
char *) resstr;
694 xmlXPathFreeContext(ctxt);
706 }
while (had_values);
static Datum values[MAXATTR]
static void PGresult * res
int errdetail(const char *fmt,...)
int errcode(int sqlerrcode)
int errmsg(const char *fmt,...)
#define ereport(elevel,...)
HeapTuple BuildTupleFromCStrings(AttInMetadata *attinmeta, char **values)
AttInMetadata * TupleDescGetAttInMetadata(TupleDesc tupdesc)
#define PG_GETARG_TEXT_PP(n)
#define PG_RETURN_TEXT_P(x)
#define PG_RETURN_FLOAT4(x)
#define PG_RETURN_BOOL(x)
void InitMaterializedSRF(FunctionCallInfo fcinfo, bits32 flags)
#define MAT_SRF_USE_EXPECTED_DESC
void heap_freetuple(HeapTuple htup)
void pfree(void *pointer)
SPITupleTable * SPI_tuptable
int SPI_exec(const char *src, long tcount)
char * SPI_getvalue(HeapTuple tuple, TupleDesc tupdesc, int fnumber)
void appendStringInfo(StringInfo str, const char *fmt,...)
void initStringInfo(StringInfo str)
Tuplestorestate * setResult
void tuplestore_puttuple(Tuplestorestate *state, HeapTuple tuple)
#define VARSIZE_ANY_EXHDR(PTR)
char * text_to_cstring(const text *t)
text * cstring_to_text(const char *s)
Datum xpath(PG_FUNCTION_ARGS)
struct PgXmlErrorContext PgXmlErrorContext
void xml_ereport(PgXmlErrorContext *errcxt, int level, int sqlcode, const char *msg)
void pg_xml_done(PgXmlErrorContext *errcxt, bool isError)
PgXmlErrorContext * pg_xml_init(PgXmlStrictness strictness)
@ PG_XML_STRICTNESS_LEGACY
static text * pgxml_result_to_text(xmlXPathObjectPtr res, xmlChar *toptag, xmlChar *septag, xmlChar *plainsep)
Datum xpath_bool(PG_FUNCTION_ARGS)
Datum xpath_number(PG_FUNCTION_ARGS)
Datum xpath_table(PG_FUNCTION_ARGS)
static xmlXPathObjectPtr pgxml_xpath(text *document, xmlChar *xpath, xpath_workspace *workspace)
static xmlChar * pgxml_texttoxmlchar(text *textstring)
PgXmlErrorContext * pgxml_parser_init(PgXmlStrictness strictness)
Datum xpath_string(PG_FUNCTION_ARGS)
static void cleanup_workspace(xpath_workspace *workspace)
Datum xml_encode_special_chars(PG_FUNCTION_ARGS)
Datum xpath_list(PG_FUNCTION_ARGS)
static xmlChar * pgxmlNodeSetToText(xmlNodeSetPtr nodeset, xmlChar *toptagname, xmlChar *septagname, xmlChar *plainsep)
Datum xpath_nodeset(PG_FUNCTION_ARGS)
PG_FUNCTION_INFO_V1(xml_encode_special_chars)