[xml] XPath, namespaces and no results. :(

I'm trying to parse specific bits of information from an xml file using the XPath syntax, without much luck. Here is a snippet of my xml file:-

<?xml version="1.0" encoding="ISO-8859-1"?>
<MELODY xmlns="http://www.foo.com"; xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"; xsi:schemaLocation="http://www.foo.com http://www.foo.com/xml_schema/melody.xsd"; Version="1" Tempo="132" Resolution="Demisemiquaver" Genre="Pop" Year="1976" Duet="Yes" >
   <TRACK Name="Player1" Artist="Bob"></TRACK>
   <TRACK Name="Player2" Artist="Tom"></TRACK>

Here is my code for querying some details of it:-

   xmlChar player1Path[] = "/MELODY/TRACK[ Name='Player1']";
   xmlXPathContextPtr context;
   xmlXPathObjectPtr result;

   context = xmlXPathNewContext(doc);
int foo = xmlXPathRegisterNs(context, BAD_CAST "", BAD_CAST "http://www.foo.com";);

       result = xmlXPathEvalExpression(player1Path, context);
       if ( !xmlXPathNodeSetIsEmpty(result->nodesetval) )
           xmlNodeSetPtr nodeset = result->nodesetval;
           if ( nodeset->nodeNr > 0 )
               buffer = xmlGetProp(nodeset->nodeTab[0], BAD_CAST "Artist");
               if ( buffer != NULL ) printf("%s\n", (const char*)buffer);

The code should search for the TRACK element, which has the attribute Name = 'Player1', then query the Artist attribute off that element. i.e. 'Bob'.

It fails however, result->nodesetval is empty. The only time I've had it work is if I don't define any namespaces in the xml file and don't register any namespaces on the context. When I look at the context structure in a debugger, the namespaces array element still remains NULL after the xmlXPathRegisterNs function call, although this function does return zero for success. Is this the correct behaviour?

Otherwise any other ideas on what I'm doing wrong?



PS I'm using the latest windows build of the libaray: 2.6.17.

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