Re: [xml] handling xpath error - libxml

On 1/28/08, Senthil Nathan <rsennat gmail com> wrote:

On 1/28/08, Stefan Behnel <stefan_ml behnel de> wrote:

Senthil Nathan wrote:
> On 1/28/08, Stefan Behnel <stefan_ml behnel de> wrote:
>> Hi,
>> Senthil Nathan wrote:
>>> I would like to how to handle the xpath error gracefully when I use the
>>> libxml api,
>>> xmlXPathEvalExpression(path, xpathCtx).
>>> If I pass a invalid path string to evaluate on the "xpathCtx", it throws
>> the
>>> error as below and stops there.
>>> But I would like to handle that error gracefully and log it accordingly
>> and
>>> proceed with my application.
>> What is the reason why you cannot just continue after this error? Just
>> call
>> xmlXPathEvalExpression() again (with a working _expression_) and everything
>> should be fine.
> In my application, when an invalid xpath string is given, during the
> xmlXPathEvalExpression( ),
> it fails with the error and just stops there. it's not continuing further
> and just hangs or stops there.
> So, I only need to break the application. Is there a better way to handle,
> in case of these xpath errors.

Ah, so it hangs *in* the eval call and does not return? I've never seen that
before. And it definitely works for me in lxml (libxml2 2.6.31):

>>> import lxml.etree as et
>>> root = et.XML("<root/>")
>>> root.xpath("/roottag///*[ check='1']")
Traceback (most recent call last):
lxml.etree.XPathEvalError: Invalid _expression_

That's basically using this code:

xpathCtxt.node = some_node;
xpathObj = xmlXPathEvalExpression(c_path, xpathCtxt);
I'm using the version 2.6.30 of libxml2
#define PACKAGE "libxml2"
#define VERSION "2.6.30"
The same way I also do it. But its only with my application which has several threads, and when I give an invalid path, just hangs. Also the CPU load is around 92% and keeps increasing further.

    xmlXPathCtxPtr xpathCtx = xmlXPathNewContext(docTree);

    xpathObj = xmlXPathEvalExpression(path, xpathCtx);


Should I need to disable some flags. Because, in case of invalid expr, its not returning from xmlXPathEvalExpression( ). If it's returns null, I can do further processing. Or am I not handling it properly. I guess so.

Could you supply the libxml2 version you are using and some example code that
shows the problem? Does your machine show high CPU load while it hangs? (i.e.
does it do something?)


I could now handle the invalid xpath error, if I use the foll. api's
    xmlXPathCompExprPtr xpathComp = xmlXPathCompile(path);
        xpathObj = xmlXPathCompiledEval(xpathComp, xpathCtx);
        printf("OTAccess::OTxmlXPathEvalExpression Invalid _expression_\n");
        return 0;
Now it doesn't dump core and the error is handled properly.
But Is it possible get that error message in a buffer string using the errno, during runtime.
"XPath error : Invalid _expression_
/roottag///,*[ check='1']"
Please help me on this.
Senthil Nathan R


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