Re: [xml] Problems using xpath with namespaces



On Thu, 07 Oct 2004 20:22:25 +0200, Pedro Rocha
<pedro_jrocha hotmail com> wrote:
Hello,

I've got the following c++ function (I've removed all the error checking):

long EvaluateXPathExpression (char * FileName, xmlChar * XPathExpression)
{
        xmlXPathContextPtr XPathContext;
        xmlXPathObjectPtr XPathObject;

        // Parse the file.
        xmlDocPtr XMLDocumentPtr = xmlParseFile (FileName);

        // Initialize the xpath context.
        XPathContext = xmlXPathNewContext (XMLDocumentPtr);

        // Register all the doc namespaces.
        char nameSpace1[] = "http://schemas.xmlsoap.org/soap/envelope/";;
        char nameSpace1_prefix[] = "soapenv";
        char nameSpace2[] =
"http://www.3gpp.org/ftp/Specs/archive/23_series/23.140/schema/REL-5-MM7-1-2";;
        xmlXPathRegisterNs (XPathContext, (xmlChar*)nameSpace1_prefix,
(xmlChar*)nameSpace1);
        xmlXPathRegisterNs (XPathContext, (xmlChar*)"", (xmlChar*)nameSpace2);

        // Eval xpath expression.
        XPathObject = xmlXPathEvalExpression (XPathExpression, XPathContext);

        return 0;
}

And the following xml file:

<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/";>
      <soapenv:Body>
            <SubmitReq
xmlns="http://www.3gpp.org/ftp/Specs/archive/23_series/23.140/schema/REL-5-MM7-1-2";>
                  <MM7Version>5.3.0</MM7Version>
            </SubmitReq>
      </soapenv:Body>
</soapenv:Envelope>

If I execute EvaluateXPathExpression("filename.xml", "//SubmitReq"), the
xpath evaluation returns no results (XPathObject->nodesetval->nodeTab is
NULL).

Any hints?

Many thanks,

Pedro

I'd suggest you give the namespace
"http://www.3gpp.org/ftp/Specs/archive/23_series/23.140/schema/REL-5-MM7-1-2";
a prefix when you call xmlXPathRegisterNs, and use it in the XPath
query. Note that you don't need to use the same prefix in XPath as you
do in the XML document.

e.g.

xmlXPathRegisterNs (XPathContext, (xmlChar*)"SomePrefix", (xmlChar*)nameSpace2);

then do something like:

EvaluateXPathExpression("filename.xml", "//SomePrefix:SubmitReq")

Stuart Dootson



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