[xml] odd xpath troubles (xmlNodeSetPtr)




I'm experiencing some really strange troubles with the xpath
facilities in libxml2.

I wrote a simple program that uses xpath to grep out some text
content from a document.  The key function goes something like this:

xmlNodeSetPtr nodes;
xmlNodePtr        current;

xpathObj = xmlXPathEvalExpression(xpathExpr, xpathCtx);
nodes = xpathObj->nodesetval;

int size = (nodes) ? nodes->nodeNr : 0;

for (int i = 0; i < size; i++) {
        current = nodes->nodeTab[i];
        printf("%s\n", current->content);
}

This compiles (and works) perfectly.  Upon reflection, though, it
occurred to me that I'd rather have this function return an
xmlNodeSetPtr to the caller and let the caller print out the nodes.
This entailed moving some of the above code out of the function and
into the caller, so now that caller does something like this:

xmlNodeSetPtr nodes;
xmlNodePtr        current;

nodes = get_raw_xml_nodes(filename, xpath);

int size = (nodes) ? nodes->nodeNr : 0;

for (int i = 0; i < size; i++) {
        current = nodes->nodeTab[i];
        printf("%s\n", current->content);
}

In other words, it does pretty much the same thing, except it grabs
the xmlNodeSetPtr from another function.  This also compiles without
a hitch.

When I run it, I get the same output as I did before, but with the
first four characters or so of each content node as junk characters
(the rest is fine).  Can anyone think of any possible reason why
this might be so?  Why would the xmlNodeSet be any different when
picked up as a return value from another function?

I tried searching through the archives, but the search function
seems to be busted at the moment.  My documents are in UTF-8.

I'm thinking the answer might be "go learn C," but I don't think so.
I've been pouring over it for hours.

Steve

-- 
Stephen Ramsay
Assistant Professor
Department of English
University of Georgia
email: sramsay uga edu
web: http://cantor.english.uga.edu/
PGP Public Key ID: 0xA38D7B11



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