RE: [xml] Context Node



// type == 1 : COUNT
// type == 2 : Element
// type == 3 : Attribute
// Note: the caller should xmlFree the result
int xtract(xmlDocPtr doc, const xmlChar *testxpath, int type, const xmlChar *attribute, xmlChar **result)
{
  int numvals=0;
  xmlXPathContextPtr ctxt = NULL;
 
  ctxt = xmlXPathNewContext(doc);
  ctxt->node = xmlDocGetRootElement(doc);
 
  xmlXPathObjectPtr obj = NULL;
  obj = xmlXPathEvalExpression(testxpath, ctxt);
 
  if((obj==NULL)||(obj->type!= XPATH_NODESET)||(obj->nodesetval==NULL)||(obj->nodesetval->nodeNr<=0))
  {
    xmlXPathFreeObject(obj);
    xmlXPathFreeContext(ctxt);
    return numvals;
  }
 
  if(type==1)
  {
    xmlXPathFreeObject(obj);
    xmlXPathFreeContext(ctxt);
    return obj->nodesetval->nodeNr;
  }
 
  //will return first value
  if(type==2)
  {
    xmlNodePtr cur = obj->nodesetval->nodeTab[0]->xmlChildrenNode;
    if(cur==NULL)
    {
      xmlXPathFreeObject(obj);
      xmlXPathFreeContext(ctxt);
      return 0;
    }
    *result = xmlStrdup(cur->content);
    xmlXPathFreeObject(obj);
    xmlXPathFreeContext(ctxt);
    return obj->nodesetval->nodeNr;
  }
 
  if(type==3)
  {
    xmlNodePtr cur = obj->nodesetval->nodeTab[0];
    if(cur==NULL)
    {
      xmlXPathFreeObject(obj);
      xmlXPathFreeContext(ctxt);
      return 0;
    }
    if(attribute==NULL)
    {
      xmlXPathFreeObject(obj);
      xmlXPathFreeContext(ctxt);
      return 0;
    }
    else
    {
      *result = xmlGetProp(cur, attribute);
      xmlXPathFreeObject(obj);
      xmlXPathFreeContext(ctxt);
      return obj->nodesetval->nodeNr;
    }
  }
  return 0;
}
-----Original Message-----
From: OrangeCrush [mailto:ocrush1 yahoo com]
Sent: Thursday, August 01, 2002 8:29 AM
To: xml gnome org
Subject: [xml] Context Node

Hello,

Does anyone know how to change the Xpath context node?  I have tried setting the xmlXpathContextPtr's current node to the desired node.  For example I defined xmllXpathContextPtr ctx and then ctx->node = mynode.  But if I use the xmlDocSetRootElement(doc,ctx-node) it sets the new root and thus I can accomplish querying a subtree.  I would like not to change the root node of the document because I am using the doc over and over again.  The file I am using is the gjobs.xml.  In this file I would like to set the xmlXpathContext structure node  member to the gjob:Contact node.

Thanks,



Do You Yahoo!?
Yahoo! Health - Feel better, live better


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