[xml] [Possibly FAQ] XPath context node/relative paths


I've looked around for answers to this, and I've seen the question
answered before, but the answers just don't seem to work for me... I'm
sure it's just due to my ignorance :]

Anyway... I have an XML tree and a given node in it, and I want to use
an XPath expression as a predicate and check whether that node
satisfies the predicate. What I'm stumbling over is setting the
context node. Without being able to set it, I can't get relative paths
to work, and I certainly can't get predicate to apply to the given

I've seen several postings claiming that one could simply do the

  context->node = my_node;
  /* Do evaluation here */

However, I simply can't get this to work... Maybe there is something
wrong elsewhere in my code? Or maybe this isn't the way to do it? Or
maybe I've misunderstood the context thing altogether?

Below is a simple program that demonstrates my problem; on my
installation it produces the following output:

  Using libxml version 2.6.11
  Shouldn't this be be 1? --> 0 (bool)
  Shouldn't this be be 1? --> 0 (node set size)

Here's the program:

#include <libxml/xpath.h>

int main(int argc, char *argv[]) {

  xmlDocPtr doc;
  xmlNodePtr root, node;

  xmlXPathContextPtr xpathCtx;
  xmlXPathObjectPtr xpathObj;

  int result;

  const xmlChar *xpathExpr = "par";

  doc = xmlNewDoc(BAD_CAST "1.0");
  root = xmlNewNode(NULL, BAD_CAST "doc");
  xmlDocSetRootElement(doc, root);
  node = xmlNewChild(root, NULL, "par", NULL);

  xpathCtx = xmlXPathNewContext(node->doc);

  xpathCtx->node = node;
  xpathObj = xmlXPathEvalExpression(xpathExpr, xpathCtx);
  result = xmlXPathEvalPredicate(xpathCtx, xpathObj);

  printf("Using libxml version %s\n", LIBXML_DOTTED_VERSION);
  printf("Shouldn't this be be 1? --> %i (bool)\n",
  printf("Shouldn't this be be 1? --> %i (node set size)\n",



Magnus Lie Hetland       Fallen flower I see / Returning to its branch
http://hetland.org       Ah! a butterfly.           [Arakida Moritake]

[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]