The error is happening deep within libxml
, on line 13479 of xpath.c
, which ends up calling xmlGenericErrorDefaultFunc()
on line 71 of error.c
, which prints to stderr
. So the easiest way to do this is to capture stderr
while the XSLT processing is going on:
- (NSXMLDocument *)transform:(NSString *)xml :(NSString *)xslt
{
NSError *xmlDocErr = nil;
NSXMLDocument *transformedXmlDoc = nil;
NSXMLDocument *xmlDoc = [[NSXMLDocument alloc]
initWithXMLString:xml
options:NSXMLDocumentValidate
error:&xmlDocErr];
if (xmlDocErr) {
NSLog(@"Error: %@", [xmlDocErr localizedDescription]);
}
else {
// Pipe for stderr
NSPipe *pipe = [NSPipe pipe];
// Duplicate of stderr (will use later)
int cntl = fcntl(STDERR_FILENO,F_DUPFD);
// Redirect stderr through our pipe
dup2([[pipe fileHandleForWriting] fileDescriptor], STDERR_FILENO);
transformedXmlDoc = [xmlDoc objectByApplyingXSLTString:xslt
arguments:nil
error:&xmlDocErr];
// Get the data
NSData *dat = [[pipe fileHandleForReading] availableData];
// Redirect stderr through our duplicate, to restore default output behavior
dup2(cntl, STDERR_FILENO);
// Did anything get logged?
if ([dat length]>0) {
NSLog(@"Error: %@", [[NSString alloc] initWithData:dat encoding:NSASCIIStringEncoding]);
}
if (xmlDocErr) {
NSLog(@"Error: %@", [xmlDocErr localizedDescription]);
}
}
return transformedXmlDoc;
}
But this is a bit of a hack, so be careful...
If you aren't satisfied with that solution, it should be possible to override the variable xmlGenericError
(which, by default, references xmlGenericErrorDefaultFunc
) with a custom error-handling function of your own using initGenericErrorDefaultFunc
on line 864 of xmlerror.h
. That would be a lot safer, but also more complicated (if it's possible at all).