| 411 | /* Core function to evaluate XPath query */ |
| 412 | |
| 413 | static xmlXPathObjectPtr |
| 414 | pgxml_xpath(text *document, xmlChar *xpath, xpath_workspace *workspace) |
| 415 | { |
| 416 | int32 docsize = VARSIZE_ANY_EXHDR(document); |
| 417 | PgXmlErrorContext *xmlerrcxt; |
| 418 | xmlXPathCompExprPtr comppath; |
| 419 | |
| 420 | workspace->doctree = NULL; |
| 421 | workspace->ctxt = NULL; |
| 422 | workspace->res = NULL; |
| 423 | |
| 424 | xmlerrcxt = pgxml_parser_init(PG_XML_STRICTNESS_LEGACY); |
| 425 | |
| 426 | PG_TRY(); |
| 427 | { |
| 428 | workspace->doctree = xmlParseMemory((char *) VARDATA_ANY(document), |
| 429 | docsize); |
| 430 | if (workspace->doctree != NULL) |
| 431 | { |
| 432 | workspace->ctxt = xmlXPathNewContext(workspace->doctree); |
| 433 | workspace->ctxt->node = xmlDocGetRootElement(workspace->doctree); |
| 434 | |
| 435 | /* compile the path */ |
| 436 | comppath = xmlXPathCompile(xpath); |
| 437 | if (comppath == NULL) |
| 438 | xml_ereport(xmlerrcxt, ERROR, ERRCODE_EXTERNAL_ROUTINE_EXCEPTION, |
| 439 | "XPath Syntax Error"); |
| 440 | |
| 441 | /* Now evaluate the path expression. */ |
| 442 | workspace->res = xmlXPathCompiledEval(comppath, workspace->ctxt); |
| 443 | |
| 444 | xmlXPathFreeCompExpr(comppath); |
| 445 | } |
| 446 | } |
| 447 | PG_CATCH(); |
| 448 | { |
| 449 | cleanup_workspace(workspace); |
| 450 | |
| 451 | pg_xml_done(xmlerrcxt, true); |
| 452 | |
| 453 | PG_RE_THROW(); |
| 454 | } |
| 455 | PG_END_TRY(); |
| 456 | |
| 457 | if (workspace->res == NULL) |
| 458 | cleanup_workspace(workspace); |
| 459 | |
| 460 | pg_xml_done(xmlerrcxt, false); |
| 461 | |
| 462 | return workspace->res; |
| 463 | } |
| 464 | |
| 465 | /* Clean up after processing the result of pgxml_xpath() */ |
| 466 | static void |
no test coverage detected