19#include <libxml/xpath.h>
20#include <libxml/tree.h>
21#include <libxml/xmlmemory.h>
22#include <libxml/xmlerror.h>
23#include <libxml/parserInternals.h>
37 xmlXPathObjectPtr
res;
43 xmlChar *toptagname, xmlChar *septagname,
47 xmlChar *septag, xmlChar *plainsep);
94 tt = xmlEncodeSpecialChars(NULL, ts);
126 buf = xmlBufferCreate();
128 if ((toptagname != NULL) && (xmlStrlen(toptagname) > 0))
130 xmlBufferWriteChar(
buf,
"<");
131 xmlBufferWriteCHAR(
buf, toptagname);
132 xmlBufferWriteChar(
buf,
">");
136 for (
i = 0;
i < nodeset->nodeNr;
i++)
138 if (plainsep != NULL)
140 xmlBufferWriteCHAR(
buf,
141 xmlXPathCastNodeToString(nodeset->nodeTab[
i]));
144 if (
i < (nodeset->nodeNr) - 1)
145 xmlBufferWriteChar(
buf, (
char *) plainsep);
149 if ((septagname != NULL) && (xmlStrlen(septagname) > 0))
151 xmlBufferWriteChar(
buf,
"<");
152 xmlBufferWriteCHAR(
buf, septagname);
153 xmlBufferWriteChar(
buf,
">");
156 nodeset->nodeTab[
i]->doc,
160 if ((septagname != NULL) && (xmlStrlen(septagname) > 0))
162 xmlBufferWriteChar(
buf,
"</");
163 xmlBufferWriteCHAR(
buf, septagname);
164 xmlBufferWriteChar(
buf,
">");
170 if ((toptagname != NULL) && (xmlStrlen(toptagname) > 0))
172 xmlBufferWriteChar(
buf,
"</");
173 xmlBufferWriteCHAR(
buf, toptagname);
174 xmlBufferWriteChar(
buf,
">");
176 result = xmlStrdup(
buf->content);
208 xmlXPathObjectPtr
res;
240 xmlXPathObjectPtr
res;
269 xmlXPathObjectPtr
res;
281 memcpy(
xpath,
"string(", 7);
283 xpath[pathsize + 7] =
')';
284 xpath[pathsize + 8] =
'\0';
309 xmlXPathObjectPtr
res;
321 fRes = xmlXPathCastToNumber(
res);
325 if (xmlXPathIsNaN(fRes))
341 xmlXPathObjectPtr
res;
353 bRes = xmlXPathCastToBoolean(
res);
364static xmlXPathObjectPtr
369 xmlXPathCompExprPtr comppath;
372 workspace->
ctxt = NULL;
373 workspace->
res = NULL;
382 if (workspace->
doctree != NULL)
384 workspace->
ctxt = xmlXPathNewContext(workspace->
doctree);
385 workspace->
ctxt->node = xmlDocGetRootElement(workspace->
doctree);
388 comppath = xmlXPathCtxtCompile(workspace->
ctxt,
xpath);
389 if (comppath == NULL)
391 "XPath Syntax Error");
394 workspace->
res = xmlXPathCompiledEval(comppath, workspace->
ctxt);
396 xmlXPathFreeCompExpr(comppath);
409 if (workspace->
res == NULL)
414 return workspace->
res;
422 xmlXPathFreeObject(workspace->
res);
423 workspace->
res = NULL;
425 xmlXPathFreeContext(workspace->
ctxt);
426 workspace->
ctxt = NULL;
428 xmlFreeDoc(workspace->
doctree);
453 xpresstr = xmlStrdup(
res->stringval);
458 xpresstr = xmlStrdup((
const xmlChar *)
"<unsupported/>");
498 const char *pathsep =
"|";
509 volatile xmlDocPtr doctree = NULL;
516 (
errcode(ERRCODE_SYNTAX_ERROR),
517 errmsg(
"xpath_table must have at least one output column")));
541 xpaths[numpaths++] = (xmlChar *) pos;
542 pos = strstr(pos, pathsep);
565 elog(
ERROR,
"xpath_table: SPI execution failed for query %s",
570 spi_tupdesc = tuptable->
tupdesc;
577 if (spi_tupdesc->
natts != 2)
580 errmsg(
"expression returning multiple columns is not valid in parameter list"),
581 errdetail(
"Expected two columns in SPI result, got %d.", spi_tupdesc->
natts)));
595 for (
i = 0;
i < proc;
i++)
599 xmlXPathContextPtr ctxt;
600 xmlXPathObjectPtr
res;
602 xmlXPathCompExprPtr comppath;
606 spi_tuple = tuptable->
vals[
i];
623 doctree = xmlReadMemory(xmldoc, strlen(xmldoc),
645 for (
j = 0;
j < numpaths;
j++)
647 ctxt = xmlXPathNewContext(doctree);
648 ctxt->node = xmlDocGetRootElement(doctree);
651 comppath = xmlXPathCtxtCompile(ctxt, xpaths[
j]);
652 if (comppath == NULL)
654 ERRCODE_INVALID_ARGUMENT_FOR_XQUERY,
655 "XPath Syntax Error");
658 res = xmlXPathCompiledEval(comppath, ctxt);
659 xmlXPathFreeCompExpr(comppath);
667 if (
res->nodesetval != NULL &&
668 rownr < res->nodesetval->nodeNr)
670 resstr = xmlXPathCastNodeToString(
res->nodesetval->nodeTab[rownr]);
679 resstr = xmlStrdup(
res->stringval);
684 resstr = xmlStrdup((
const xmlChar *)
"<unsupported/>");
691 values[
j + 1] = (
char *) resstr;
693 xmlXPathFreeContext(ctxt);
705 }
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)
text * cstring_to_text(const char *s)
char * text_to_cstring(const text *t)
Datum xpath(PG_FUNCTION_ARGS)
struct PgXmlErrorContext PgXmlErrorContext
PgXmlErrorContext * pg_xml_init(PgXmlStrictness strictness)
void xml_ereport(PgXmlErrorContext *errcxt, int level, int sqlcode, const char *msg)
void pg_xml_done(PgXmlErrorContext *errcxt, bool isError)
@ 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)