GNOME Bugzilla – Bug 126793
structured error from xmlXPathCompile lacks some info
Last modified: 2009-08-15 18:40:50 UTC
If xmlXPathCompile(comp) fails, it doesn't provide exact position of compilation failure to a structured handler (via int1 and str1). On the other hand, xmlXPathEvalExpr() or xmlXPathEval() work fine. Below is a dump of xmlError structures I get: xmlXPathEvalExpr() or xmlXPathEval() (OK): domain: 12 code: 1207 message: Invalid expression\n level: ERROR file: line: 0 str1: foo::bar str2: str3: int1: 4 int2: 0 ctxt: 0 node: 0 xmlXPathCompile (BAD): domain: 12 code: 1207 message: Invalid expression\n level: ERROR file: line: 0 str1: str2: str3: int1: 0 int2: 0 ctxt: 0 node: 0
Similarly: parser reports no structured errors at all via a registered structured error handler (this problem affects both XML parsing errors or validation errors). Well-formedness errors can be taken from parser ctxt->lastError at the end of parsing but validation errors can't (ctxt->lastError is ERR_OK, only ctxt->valid == 0 indicates a validity error). Non-structured errors are repotred in both cases. Here is a simple C code to test with (if given an argument, parses that file, otherwise parses the inline XML document). __BEGIN_TEST_CASE___ #include <stdarg.h> #include <unistd.h> #include <libxml/parser.h> #include <libxml/parserInternals.h> #include <libxml/xmlerror.h> #include <libxml/tree.h> char* xmldoc = "<?xml version='1.0'?>" "<!DOCTYPE boo []>" "<foo/>"; void my_flat_err(void *ctx, const char *msg, ...) { printf("<Flat-error-invoked>\n"); va_list ap; va_start(ap, msg); vfprintf(stderr, msg, ap); va_end(ap); printf("</Flat-error-invoked>\n"); } void my_struct_err(void *userData, xmlErrorPtr err) { printf("<structured-error-invoked/>\n"); } int main (int argn, char** argv) { int ret; xmlParserCtxtPtr ctxt; xmlDocPtr doc; xmlSetGenericErrorFunc( NULL , (xmlGenericErrorFunc) my_flat_err); xmlSetStructuredErrorFunc( NULL , (xmlStructuredErrorFunc)my_struct_err); if (argn<=1) { ctxt = xmlCreateMemoryParserCtxt((const char*)xmldoc, strlen(xmldoc)); if ( ctxt->input != NULL ) { ctxt->input->filename = xmlStrdup((const xmlChar *) ""); } xmlCtxtUseOptions(ctxt, XML_PARSE_DTDVALID); ret = xmlParseDocument(ctxt); } else { ctxt = xmlNewParserCtxt(); doc = xmlCtxtReadFile(ctxt, argv[1], NULL, XML_PARSE_DTDVALID); } if (ctxt->valid == 0) printf("novalid\n"); doc = ctxt->myDoc; xmlFreeParserCtxt(ctxt); /* doc = xmlReadMemory(xmldoc, strlen(xmldoc), NULL, NULL, XML_PARSE_DTDVALID); */ if (doc) xmlFree(doc); }
Oh, also forgot to mentioned: Daniel, the non-structured error handler is still called several times per error as with old libxml2 versions, while you once told me on the list that libxml2.6.x's behaviour was to call the handler only once per an error (at least that's how I understood it). Thanks, Petr
Okay, I have tried to fix the structured error handling problems you raised . It should be fixed in CVS now: paphio:~/XML -> gcc -o tst -Iinclude tst.c .libs/libxml2.a -lm -lz -lpthread paphio:~/XML -> ./tst <structured-error-invoked/> novalid paphio:~/XML -> including the XPath reporting glitches (but untested). See also bug #126211 which is related. For the non-structured error handler there is still multiple callbacks but the number is bounded per error, it used in some case to be called once per character printed in the context, that I tried to fix. If the issue is really critical, I could try to fix this, but I would rather get the structured handler ready instead. Daniel
This should be closed in release of libxml2-2.6.5, Daniel