Version of XSLT in iPhone

I plan to use XML/XSLT in my iPhone application.

What version of XSLT is currently supported on the iPhone? Can I use XSLT 2.0 or just 1.0 ?


Using libxslt on the iPhone OS is actually quite easy:

  1. Download the source-code of libxslt and extract it.
  2. Add the "libxslt" dir to Header search paths in your build settings. Also, add the path to the libxml-headers there (usually /Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS3.0.sdk/usr/include/libxml2).
  3. Add libxml2.2.dylib and libxslt.dylib to the linked frameworks (Xcode "Groups & Files" panel: right click on "Frameworks" --> "Add" --> "Existing Frameworks...").
  4. Create a simple XML file and its XSL tranformation.

And finally you can use a code similar to the sample above to get the tranformation result into an NSString (e.g. to display in in a UIWebView):

#import <libxml/xmlmemory.h>
#import <libxml/debugXML.h>
#import <libxml/HTMLtree.h>
#import <libxml/xmlIO.h>
#import <libxml/xinclude.h>
#import <libxml/catalog.h>
#import <libxslt/xslt.h>
#import <libxslt/xsltInternals.h>
#import <libxslt/transform.h>
#import <libxslt/xsltutils.h>

...

NSString* filePath = [[NSBundle mainBundle] pathForResource: @"article" ofType: @"xml"];
NSString* styleSheetPath = [[NSBundle mainBundle] pathForResource: @"article_transform" ofType:@"xml"];

xmlDocPtr doc, res;

// tells the libxml2 parser to substitute entities as it parses your file
xmlSubstituteEntitiesDefault(1);
// This tells libxml to load external entity subsets
xmlLoadExtDtdDefaultValue = 1;

sty = xsltParseStylesheetFile((const xmlChar *)[styleSheetPath cStringUsingEncoding: NSUTF8StringEncoding]);
doc = xmlParseFile([filePath cStringUsingEncoding: NSUTF8StringEncoding]);
res = xsltApplyStylesheet(sty, doc, NULL);

char* xmlResultBuffer = nil;
int length = 0;

xsltSaveResultToString(&xmlResultBuffer, &length, res, sty);

NSString* result = [NSString stringWithCString: xmlResultBuffer encoding: NSUTF8StringEncoding];

NSLog(@"Result: %@", result);

free(xmlResultBuffer);

xsltFreeStylesheet(sty);
xmlFreeDoc(res);
xmlFreeDoc(doc);

xsltCleanupGlobals();
xmlCleanupParser();

As Lou Franco points out, you're not allowed dylibs on the iPhone.

It'll work fine in development both in the simulator and the phone but it'll be rejected as soon as you submit it to Apple for approval. My app was rejected within about 20 minutes, presumably by their automated static analysis tool.

Instead, download the source, add it to your project, link the LibXML2.2.dylib (don't ask me why this dylib is allowed but the XSLT isn't!) and build the project. That's pretty much all you have to do. Credit to Lou for this as it was he who pointed me in the right direction.


I'm afraid the xslt situation is rather grim. The NSXMLDocument class would be the way to do this but Apple pulled it from the iPhone.

TouchXML plans xslt support but doesn't have it yet.

The only option I know of is to directly use libxslt, which supports xslt 1.0 and some of the exslt extensions.