Q:

libxml x path expression not interpreted

I've got the following xml text
<?xml version="1.0" encoding="UTF-8"?>
<projects type="array">
  <project>
    <created-on type="date">2012-01-17</created-on>
    <id type="integer">8860795</id>
    <last-changed-on type="datetime">2012-01-17T01:37:25Z</last-changed-on>
  </project>
</projects>

So there is just one project. But I want to count the projects. In an online xpath evaluator my expression works fine and 1 is returned. But not in my libxml method. I use the following xpath expresseion:
count(/projects/project)

and the following objective c method for evaluation of the expression:
-(NSString*) getValueForXPathExpression:(NSString*) xPathExp_ andXMLDoc:(NSData*) xml
{
xmlDoc* doc = xmlParseMemory( [xml bytes], [xml length]);

const unsigned char* xPathExp = (const unsigned char*) [xPathExp_cStringUsingEncoding:NSUTF8StringEncoding];
xmlXPathContextPtr xpathCtx = xmlXPathNewContext(doc);
xmlXPathObjectPtr xpathObj = xmlXPathEvalExpression( xPathExp, xpathCtx);
xmlNodeSetPtr nodeSetPtr = xpathObj->nodesetval;
NSString* value;
if( nodeSetPtr != 0 ) {
    xmlNode* node = nodeSetPtr->nodeMax > 0 ? nodeSetPtr->nodeTab[0] : 0;
    if( node != 0 && [[self stringFromCString:node->name] isEqualToString:@"text"] ) 
        value = [self stringFromCString:node->content];
    else
        value = [NSString stringWithFormat:@"Node for xpath Exp: %@ does not exist or is not of type text.", xPathExp_];
} else
    value = [NSString stringWithFormat:@"nodeSetPtr for %@ is null.", xPathExp_];

//XML-Parser aufräumen
xmlFreeDoc(doc);
xmlCleanupParser();

return value;
}

The return value is
nodeSetPtr for count(/projects/project) is null

What am I doing wrong here?
Edit: I checked that xpathCtx and xpathObj are not null but xpathObj->nodesetval is.
Edit2: Okay I found out. Count returns a number and not a string, therefore xpathObj->nodesetval is null but xpathObj->floatval is 1, the value I wanted. Actually quite logical since there can't be any nodeset :)

A:

Have you checked the value of "doc" to make sure it is not NULL before trying to set up an XPath context? It looks to me like the xmlParseMemory function is expecting a null-terminated buffer, and I don't believe your [xml bytes] will provide that. Also, you don't show how your NSData containing the XML is created so it's impossible to see if you've got it encoded in a way that xmlParseMemory can handle it.
Assuming that the document is successfully being parsed, the next thing I would check is the type of the value returned from the Xpath evaluation. In this case, look at xpathObj->type. Unless the type is a nodeset (XPATH_NODESET), then getting the xpathObj->nodesetval isn't going to help you. I suspect the result type is XPATH_NUMBER, and that xpathObj->floatValue will return the correct count value of 1.
Based on the logic you have after the evaluation, it looks like you might want more than just the count though. You might just want to use an XPath of /projects/project and loop through the node set that gets returned

