Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
577 views
in Technique[技术] by (71.8m points)

cocoa touch - 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 ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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();

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...