iPhone版XSLT [英] Version of XSLT in iPhone
本文介绍了iPhone版XSLT的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我计划在我的iPhone应用程序中使用XML / XSLT。
I plan to use XML/XSLT in my iPhone application.
iPhone目前支持哪种版本的XSLT?我可以使用XSLT 2.0或1.0吗?
What version of XSLT is currently supported on the iPhone? Can I use XSLT 2.0 or just 1.0 ?
推荐答案
使用 libxslt
iPhone OS实际上很容易:
Using libxslt
on the iPhone OS is actually quite easy:
- 下载libxslt的源代码,然后解压缩。
- 将标题搜索路径添加到libxslt目录您的构建设置。另外,在那里添加libxml-headers的路径(通常
/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS3.0.sdk/usr/include/libxml2
)。 - 将libxml2.2.dylib和libxslt.dylib添加到链接框架(XcodeGroups& Files面板中:右键单击Frameworks - >添加 - >现有框架...)。
- 创建一个简单的XML文件及其XSL格式。
- Download the source-code of libxslt and extract it.
- 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
). - Add libxml2.2.dylib and libxslt.dylib to the linked frameworks (Xcode "Groups & Files" panel: right click on "Frameworks" --> "Add" --> "Existing Frameworks...").
- Create a simple XML file and its XSL tranformation.
最后,您可以使用类似于上面示例的代码将转换结果转换为 NSString
(例如,在 UIWebView
中):
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();
这篇关于iPhone版XSLT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文