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>
40 xmlXPathObjectPtr
res;
46 xmlChar *toptagname, xmlChar *septagname,
50 xmlChar *septag, xmlChar *plainsep);
97 tt = xmlEncodeSpecialChars(NULL, ts);
129 buf = xmlBufferCreate();
131 if ((toptagname != NULL) && (xmlStrlen(toptagname) > 0))
133 xmlBufferWriteChar(
buf,
"<");
134 xmlBufferWriteCHAR(
buf, toptagname);
135 xmlBufferWriteChar(
buf,
">");
139 for (
i = 0;
i < nodeset->nodeNr;
i++)
141 if (plainsep != NULL)
143 xmlBufferWriteCHAR(
buf,
144 xmlXPathCastNodeToString(nodeset->nodeTab[
i]));
147 if (
i < (nodeset->nodeNr) - 1)
148 xmlBufferWriteChar(
buf, (
char *) plainsep);
152 if ((septagname != NULL) && (xmlStrlen(septagname) > 0))
154 xmlBufferWriteChar(
buf,
"<");
155 xmlBufferWriteCHAR(
buf, septagname);
156 xmlBufferWriteChar(
buf,
">");
159 nodeset->nodeTab[
i]->doc,
163 if ((septagname != NULL) && (xmlStrlen(septagname) > 0))
165 xmlBufferWriteChar(
buf,
"</");
166 xmlBufferWriteCHAR(
buf, septagname);
167 xmlBufferWriteChar(
buf,
">");
173 if ((toptagname != NULL) && (xmlStrlen(toptagname) > 0))
175 xmlBufferWriteChar(
buf,
"</");
176 xmlBufferWriteCHAR(
buf, toptagname);
177 xmlBufferWriteChar(
buf,
">");
179 result = xmlStrdup(
buf->content);
211 xmlXPathObjectPtr res;
243 xmlXPathObjectPtr res;
272 xmlXPathObjectPtr res;
284 memcpy(
xpath,
"string(", 7);
286 xpath[pathsize + 7] =
')';
287 xpath[pathsize + 8] =
'\0';
312 xmlXPathObjectPtr res;
324 fRes = xmlXPathCastToNumber(res);
328 if (xmlXPathIsNaN(fRes))
344 xmlXPathObjectPtr res;
356 bRes = xmlXPathCastToBoolean(res);
367static xmlXPathObjectPtr
372 xmlXPathCompExprPtr comppath;
375 workspace->
ctxt = NULL;
376 workspace->
res = NULL;
385 if (workspace->
doctree != NULL)
387 workspace->
ctxt = xmlXPathNewContext(workspace->
doctree);
388 workspace->
ctxt->node = xmlDocGetRootElement(workspace->
doctree);
391 comppath = xmlXPathCtxtCompile(workspace->
ctxt,
xpath);
392 if (comppath == NULL)
394 "XPath Syntax Error");
397 workspace->
res = xmlXPathCompiledEval(comppath, workspace->
ctxt);
399 xmlXPathFreeCompExpr(comppath);
412 if (workspace->
res == NULL)
417 return workspace->
res;
425 xmlXPathFreeObject(workspace->
res);
426 workspace->
res = NULL;
428 xmlXPathFreeContext(workspace->
ctxt);
429 workspace->
ctxt = NULL;
431 xmlFreeDoc(workspace->
doctree);
456 xpresstr = xmlStrdup(res->stringval);
460 elog(
NOTICE,
"unsupported XQuery result: %d", res->type);
461 xpresstr = xmlStrdup((
const xmlChar *)
"<unsupported/>");
501 const char *pathsep =
"|";
512 volatile xmlDocPtr doctree = NULL;
519 (
errcode(ERRCODE_SYNTAX_ERROR),
520 errmsg(
"xpath_table must have at least one output column")));
544 xpaths[numpaths++] = (xmlChar *) pos;
545 pos = strstr(pos, pathsep);
568 elog(
ERROR,
"xpath_table: SPI execution failed for query %s",
573 spi_tupdesc = tuptable->
tupdesc;
580 if (spi_tupdesc->
natts != 2)
583 errmsg(
"expression returning multiple columns is not valid in parameter list"),
584 errdetail(
"Expected two columns in SPI result, got %d.", spi_tupdesc->
natts)));
598 for (
i = 0;
i < proc;
i++)
602 xmlXPathContextPtr ctxt;
603 xmlXPathObjectPtr res;
605 xmlXPathCompExprPtr comppath;
609 spi_tuple = tuptable->
vals[
i];
626 doctree = xmlReadMemory(xmldoc, strlen(xmldoc),
648 for (
j = 0;
j < numpaths;
j++)
650 ctxt = xmlXPathNewContext(doctree);
651 ctxt->node = xmlDocGetRootElement(doctree);
654 comppath = xmlXPathCtxtCompile(ctxt, xpaths[
j]);
655 if (comppath == NULL)
657 ERRCODE_INVALID_ARGUMENT_FOR_XQUERY,
658 "XPath Syntax Error");
661 res = xmlXPathCompiledEval(comppath, ctxt);
662 xmlXPathFreeCompExpr(comppath);
670 if (res->nodesetval != NULL &&
671 rownr < res->nodesetval->nodeNr)
673 resstr = xmlXPathCastNodeToString(res->nodesetval->nodeTab[rownr]);
682 resstr = xmlStrdup(res->stringval);
686 elog(
NOTICE,
"unsupported XQuery result: %d", res->type);
687 resstr = xmlStrdup((
const xmlChar *)
"<unsupported/>");
694 values[
j + 1] = (
char *) resstr;
696 xmlXPathFreeContext(ctxt);
708 }
while (had_values);
static Datum values[MAXATTR]
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)
PG_MODULE_MAGIC_EXT(.name="xml2",.version=PG_VERSION)
Datum xpath_nodeset(PG_FUNCTION_ARGS)
PG_FUNCTION_INFO_V1(xml_encode_special_chars)