在 Flex 中执行 XML 转换 [英] Performing XML Transformations in Flex
问题描述
我希望能够在我的 AIR 项目中使用 xslt 文件运行 xml 转换.实现这一目标的最佳方法是什么?
在 AIR 1.5 中,包含一个支持 XSLT 的 Webkit 版本.
使用 JavaScript 中的 XSLTProcessor
类,就像在 Firefox 中一样.(注意:有一个烦人的错误.样式表不能包含不间断空格,无论是字面意思还是字符引用.我听说更新的 Webkit 版本会解决这个问题.)
下面是一个完整的例子.
创建一个文件test.html
<头><title>XSLT 测试</title><script type="text/javascript">//<!--功能测试() {//第一步:解析样式表var 样式表= "<xsl:transform xmlns:xsl='http://www.w3.org/1999/XSL/Transform'"+ " version='1.0'>"+ " <xsl:template match='/'>"+来自 XSLT 的 Hello World!"+ " </xsl:template>"+ "</xsl:transform>";var 样式表文档= new DOMParser().parseFromString(stylesheet, "application/xml");//第二步:解析源文档var source = " ";var 源文档= new DOMParser().parseFromString(source, "application/xml");//步骤 3:执行 XSL 转换var xslt = new XSLTProcessor();xslt.importStylesheet(stylesheetDocument);var newFragment = xslt.transformToFragment(sourceDocument, document);//第 4 步:显示结果document.body.appendChild(newFragment.firstChild);}//-->头部><身体><input type="submit" onclick="test()">输出:</html>
和一个文件 test.xml
(来源:lichteblau.com) >I'd like to be able to run an xml transformation using an xslt file in my AIR project. What's the best way to accomplish this?
解决方案 In AIR 1.5, a version of Webkit with support for XSLT is included.
Use the class XSLTProcessor
from JavaScript just like you would in Firefox. (Note: There is one annoying bug. Stylesheets cannot contain non-breaking spaces, no matter whether literally or as a character reference. I am told that more recent versions of Webkit will fix this issue.)
Below is a complete example.
Create a file test.html
<html>
<head>
<title>XSLT test</title>
<script type="text/javascript">
// <!--
function test() {
// Step 1: Parse the stylesheet
var stylesheet
= "<xsl:transform xmlns:xsl='http://www.w3.org/1999/XSL/Transform'"
+ " version='1.0'>"
+ " <xsl:template match='/'>"
+ " Hello World from XSLT!"
+ " </xsl:template>"
+ "</xsl:transform>";
var stylesheetDocument
= new DOMParser().parseFromString(stylesheet, "application/xml");
// Step 2: Parse the source document
var source = "<dummy/>";
var sourceDocument
= new DOMParser().parseFromString(source, "application/xml");
// Step 3: Perform the XSL transformation
var xslt = new XSLTProcessor();
xslt.importStylesheet(stylesheetDocument);
var newFragment = xslt.transformToFragment(sourceDocument, document);
// Step 4: Show the result
document.body.appendChild(newFragment.firstChild);
}
// -->
</script>
</head>
<body>
<input type="submit" onclick="test()">
Output:
</body>
</html>
and a file test.xml
<application xmlns="http://ns.adobe.com/air/application/1.0">
<id>test</id>
<filename>test</filename>
<initialWindow>
<content>test.html</content>
<visible>true</visible>
</initialWindow>
</application>
You can try it using the debugging runtime, for example:
adl test.xml
Klick the button, and it will say:
(source: lichteblau.com)
这篇关于在 Flex 中执行 XML 转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!