Search code examples
xmlxpathlibxml2

does libxml2 have available a description of XPath syntax error (XML_XPATH_EXPR_ERROR)


I'm implementing LibXML2 XPath in my code.

MSEXPORT int GetXpathXML(xmlDocPtr doc, xmlNodePtr node, xmlChar* xpathExpr, NodeRefsHdl NodeRefs)
{
#define ERR_OUT(x) {xmlXPathFreeObject(xpathObj); xmlXPathFreeContext(xpathCtx); return x;}
  if (doc == NULL) return(-1);

  /* Create xpath evaluation context */
  xmlXPathContextPtr xpathCtx = xmlXPathNewContext(doc);
  if(xpathCtx == NULL) return(-2);

  /* Evaluate xpath expression. */
  xmlXPathObjectPtr xpathObj = xmlXPathNodeEval((node == NULL ? xmlDocGetRootElement(doc): node), xpathExpr, xpathCtx);

  if(xpathObj == NULL) {
      snprintf(dan, BUFSZ, "Error %d: unable to evaluate xpath expression \"%s\"\n", (int) (xpathCtx->error), xpathExpr); DEBUG_MSG(dan);
      ERR_OUT(-3);
  }

When I intentionally send this bad XPath:

/*/*(1

(xpathCtx->error) yields 0 and xmlGetLastError() yields me XML_XPATH_EXPR_ERROR

But oXygen gives me:

XPath failed due to: expected ")", found "<eof>"

Can I retrieve more XPath syntax error information from LibXML2?


Solution

  • The relevant part of the source code can be found in xmlXPathCompPrimaryExpr:

        if (CUR != ')') {
            XP_ERROR(XPATH_EXPR_ERROR);
        }
    

    So the answer is no. The error code is all the information libxml2's XPath expression parser provides.