[xml] XPath doing a search on a subtree



Hi, 

I have a xmlNodePtr pointing to a particular node in a DOM tree.  
I'd like to execute XPath relative search on subtree from that node down
(i.e. on the subtree for which this node is the top node). In particular
I'd like to find all recurerrences of a particular node within this
subtree. 

So far I've tried code below, but it does a search for entire DOM, not
just a relative subtree (i.e. from a particular node down):

I would appreciate your help in what might be causing this to execute
for every node in DOM, instead just for subtree as pointed by current
(referent) node. 

Thanks,
Milan



Code:

Call from app:

xmlNodeSetPtr xmlActionNodeSet = NULL ;
                        bRet =
SelectXMLNodeCollection_RelativePath(xmlCurrentModel, "//Model",
xmlActionNodeSet) ;     // assume that there is a subtree with Model
nodes

Top level function:

bool SelectXMLNodeCollection_RelativePath(xmlNodePtr xmlRefNode, 
string strNodePath, xmlNodeSetPtr & xmlNodeSet)
{
        bool bRet = true ; 

        try
        {
                xmlChar *xpath = (xmlChar*)(strNodePath.c_str());
                
                xmlXPathObjectPtr xmlSelectionResult;

                xmlSelectionResult = xmlGetRelativePathNodeSet
(xmlRefNode, xpath);
                if (xmlSelectionResult)
                {                       
                        xmlNodeSet = xmlSelectionResult->nodesetval;

                }
        }
        catch(...)
        {
                bRet = false ; 
        }

        return bRet ;                   
}

Helper f-n:

xmlXPathObjectPtr xmlGetRelativePathNodeSet (xmlNodePtr xmlNode, xmlChar
*xpath)
{       
        xmlXPathContextPtr context;
        xmlXPathObjectPtr result;

        context = xmlXPathNewContext(xmlNode->doc);
        if (context == NULL) {
                printf("Error in xmlXPathNewContext\n");
                return NULL;
        }

        context->node = xmlNode ; 

        result = xmlXPathEval(xpath, context);
        xmlXPathFreeContext(context);
        if (result == NULL) {
                printf("Error in xmlXPathEvalExpression\n");
                return NULL;
        }
        if(xmlXPathNodeSetIsEmpty(result->nodesetval)){
                xmlXPathFreeObject(result);
                printf("No result\n");
                return NULL;
        }
        return result;
}



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