iPhone 中的 XSLT 版本 [英] Version of XSLT in iPhone

查看:21
本文介绍了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 ?

推荐答案

在 iPhone OS 上使用 libxslt 其实很简单:

Using libxslt on the iPhone OS is actually quite easy:

  1. 下载 libxslt 的源代码 并解压.
  2. 将libxslt"目录添加到构建设置中的标头搜索路径.另外,在那里添加 libxml-headers 的路径(通常是 /Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS3.0.sdk/usr/include/libxml2).
  3. 将 libxml2.2.dylib 和 libxslt.dylib 添加到链接的框架(Xcode组和文件"面板:右键单击框架"-->添加"-->现有框架...").
  4. 创建一个简单的 XML 文件及其 XSL 转换.
  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.

最后,您可以使用类似于上面示例的代码将转换结果转换为 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆