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 ?
In XSLT, call system-property('xsl:version') . It will return 1.0 or 2.0 depending on whether you are using a 1.0 or 2.0 processor.
For this you need a schema-aware XSLT processor that does static checking (for example Saxon-EE), and you need the stylesheet to (a) import the schema using xslt:import-schema, and (b) to invoke validation on the result elements using [xsl:]validation="strict".
XSLT 3.0 specifies extensions to the XDM 3.0 data model, to the XPath 3.0 language syntax, and to the XPath 3.0 function library to underpin the introduction of maps, which were found necessary to support some XSLT streaming use cases, to enable XSLT to process JSON data, and to make many other processing tasks easier.
Using libxslt
on the iPhone OS is actually quite easy:
/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS3.0.sdk/usr/include/libxml2
).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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With