2009-01-16 23 views

回答

3

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

從JavaScript中使用類XSLTProcessor就像在Firefox中一樣。 (注意:有一個令人討厭的bug,Stylesheets不能包含非空格,不管是字面還是字符引用,我被告知更新版本的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 

KLICK按鈕,它會說:

example http://www.lichteblau.com/tmp/stackoverflow-xslt.png

1

XSLT支持通常由瀏覽器提供。嵌入在AIR中的Webkit版本不支持XSLT。所以,你必須自己做這一切。我發現this項目可讓您在AS3中使用XPath查詢。現在,您必須自己完成模板解析和節點創建。