简体   繁体   中英

Issue finding a namespaced node in an XML document using libxml2's XPath

Given the following document, I want to extract tns:EchoResponse . (Original document prettified for readability.)

<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope 
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
  xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
    <soap:Body>
        <tns:EchoResponse 
          xmlns:tns="http://www.apis-it.hr/fin/2012/types/f73" 
          xsi:schemaLocation="http://www.apis-it.hr/fin/2012/types/f73 FiskalizacijaSchema.xsd "
          >
            hello
         </tns:EchoResponse>
    </soap:Body>
</soap:Envelope>

I've parsed the document, I've created an XPath context, I've registered the namespaces soap and tns , and then I've run the query using the following XPath expression.

//soap:Envelope[1]/soap:Body[1]/tns:EchoResponse[1]

Unfortunately, only lookup that ignores namespaces and uses only the local name seems to work:

//*[local-name(.) = 'Envelope']/*[local-name(.) = 'Body'][1]/*[local-name(.) = 'EchoResponse'][1]

So… what did I do wrong? What did I assume incorrectly? Here's the snippet of the code that deals with this document.

    xmlDocPtr xmldoc = xmlReadMemory(databuffer, datasize, ISFEndpointURL(), NULL, 0);
    if(!xmldoc)
    {
        err = -1;
        goto finalize;
    }

    xmlXPathContextPtr xpathCtx = xmlXPathNewContext(xmldoc);
    if(!xpathCtx)
    {
        err = -1;
        xmlFreeDoc(xmldoc);
        goto finalize;
    }

    xmlXPathRegisterNs(xpathCtx, BAD_CAST "soap", BAD_CAST "http://schemas.xmlsoap.org/soap/envelope");
    xmlXPathRegisterNs(xpathCtx, BAD_CAST "tns", BAD_CAST "http://www.apis-it.hr/fin/2012/types/f73");

    // //soap:Envelope[1]/soap:Body[1]/*[local-name(.) = 'EchoResponse']
    xmlXPathObjectPtr xpathObj = xmlXPathEvalExpression(BAD_CAST "./*[local-name(.) = 'Envelope']/*[local-name(.) = 'Body'][1]/*[local-name(.) = 'EchoResponse'][1]", xpathCtx);
    if(!xpathObj)
    {
        err = -1;
        xmlXPathFreeContext(xpathCtx);
        xmlFreeDoc(xmldoc);
        goto finalize;
    }
    if(xmlXPathNodeSetIsEmpty(xpathObj->nodesetval)){
        err = -1;
        xmlXPathFreeObject(xpathObj);
        xmlXPathFreeContext(xpathCtx);
        xmlFreeDoc(xmldoc);
        printf("%s: No XPath result\n", __FUNCTION__);
        goto finalize;
    }


    if(strcmp((char *)xmlXPathCastNodeSetToString(xpathObj->nodesetval), echoText))
    {
        err = 1;
        xmlXPathFreeObject(xpathObj);
        xmlXPathFreeContext(xpathCtx);
        xmlFreeDoc(xmldoc);
        printf("%s: Incorrect echo result: %s\n", __FUNCTION__, xpathObj->stringval);
        goto finalize;
    }

    xmlXPathFreeObject(xpathObj);
    xmlXPathFreeContext(xpathCtx);

The soap prefix in the XML document is bound to the namespace URI

http://schemas.xmlsoap.org/soap/envelope/

whereas your code binds the soap prefix in the XPath context to a different URI

http://schemas.xmlsoap.org/soap/envelope

The XPath doesn't match anything because it is looking for the wrong namespace.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM