Question

I've written a C++ wrapper function for libxml2 that makes it easy for me to do queries on an XML document:

bool XPathQuery(
    const std::string& doc,
    const std::string& query,
    XPathResults& results); 

But I have a problem: I need to be able to do another XPath query on the results of my first query.

Currently I do this by storing the entire subdocument in my XPathResult object, and then I pass XPathResult.subdoc into the XPathQuery function. This is awfully inefficient.

So I'm wondering ... does libxml2 provide anything that would make it easy to store the context of an xpath query (a reference to a node, perhaps?) and then perform another query using that reference as the xpath root?

Was it helpful?

Solution

You should reuse the xmlXPathContext and just change its node member.

#include <stdio.h>
#include <libxml/xpath.h>
#include <libxml/xmlerror.h>

static xmlChar buffer[] = 
"<?xml version=\"1.0\"?>\n<foo><bar><baz/></bar></foo>\n";

int
main()
{
  const char *expr = "/foo";

  xmlDocPtr document = xmlReadDoc(buffer,NULL,NULL,XML_PARSE_COMPACT);
  xmlXPathContextPtr ctx = xmlXPathNewContext(document);
  //ctx->node = xmlDocGetRootElement(document);

  xmlXPathCompExprPtr p = xmlXPathCtxtCompile(ctx, (xmlChar *)expr);
  xmlXPathObjectPtr res = xmlXPathCompiledEval(p, ctx);

  if (XPATH_NODESET != res->type)
    return 1;

  fprintf(stderr, "Got object from first query:\n");
  xmlXPathDebugDumpObject(stdout, res, 0);
  xmlNodeSetPtr ns = res->nodesetval;
  if (!ns->nodeNr)
    return 1;
  ctx->node = ns->nodeTab[0];
  xmlXPathFreeObject(res);

  expr = "bar/baz";
  p = xmlXPathCtxtCompile(ctx, (xmlChar *)expr);
  res = xmlXPathCompiledEval(p, ctx);

  if (XPATH_NODESET != res->type)
    return 1;
  ns = res->nodesetval;
  if (!ns->nodeNr)
    return 1;
  fprintf(stderr, "Got object from second query:\n");
  xmlXPathDebugDumpObject(stdout, res, 0);

  xmlXPathFreeContext(ctx);
  return 0;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top