在Flex中执行XML转换

问题描述 投票:2回答:2

我希望能够在AIR项目中使用xslt文件运行xml转换。实现这一目标的最佳方法是什么?

xml flex xslt air
2个回答
3
投票

在AIR 1.5中,包含了一个支持XSLT的Webkit版本。

使用JavaScript中的类XSLTProcessor就像在Firefox中一样。 (注意:有一个令人讨厌的错误。样式表不能包含不间断的空格,无论是字面上还是字符引用。我被告知更新版本的Webkit将解决这个问题。)

以下是一个完整的例子。

创建一个文件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>

和一个文件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>

您可以使用调试运行时尝试它,例如:

adl test.xml

点击按钮,它会说:

example (来源:lichteblau.com


1
投票

XSLT支持通常由浏览器提供。嵌入AIR的Webkit版本不支持XSLT。所以,你必须自己做这一切。我发现this项目可以让你在AS3中使用XPath查询。现在,您需要自己完成模板解析和节点创建。

© www.soinside.com 2019 - 2024. All rights reserved.