Posted By

chrisaiv on 11/20/08


Tagged

xslt xml AIR Flex as3


Versions (?)

AIR: AIR for XSTL


 / Published in: MXML
 

I found this blog that explained how AIR can transform XML using XSLT. http://blogs.adobe.com/briggs/2008/05/usingairforxsltprocessing.html

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <mx:WindowedApplication
  3. xmlns:mx ="http://www.adobe.com/2006/mxml"
  4. creationComplete ="init()"
  5. width ="640"
  6. height ="800"
  7. horizontalAlign ="left" viewSourceURL="srcview/index.html">
  8.  
  9. <mx:Script>
  10. <![CDATA[
  11.  
  12. import mx.controls.*;
  13.  
  14. private var html:HTMLLoader = new HTMLLoader();
  15.  
  16. private function init():void
  17. {
  18. // Load the HTML page that has the JS that we'll use to
  19. // do the XSLT.
  20. var urlReq:URLRequest = new URLRequest("xslt.html");
  21. html.load(urlReq);
  22.  
  23. // Assign some sample content to the text boxes.
  24. xmlArea.text = sampleXML;
  25. xslArea.text = sampleXSL;
  26. }
  27.  
  28. private function process():void
  29. {
  30. var doProcess:Boolean = true;
  31. try
  32. {
  33. // Validate the input XML.
  34. var xml:XML = new XML(xmlArea.text);
  35. if (xml == null || xml.toXMLString().length == 0)
  36. {
  37. Alert.show("Please enter some XML to transform.");
  38. doProcess = false;
  39. }
  40. }
  41. catch (e:Error)
  42. {
  43. Alert.show("The XML to transform is malformed.");
  44. doProcess = false;
  45. }
  46. if (doProcess)
  47. {
  48. try
  49. {
  50. // Validate the input XSLT.
  51. var xsl:XML = new XML(xslArea.text);
  52. if (xsl == null || xsl.toXMLString().length == 0)
  53. {
  54. Alert.show("Please enter an XSLT document.");
  55. doProcess = false;
  56. }
  57. }
  58. catch (e:Error)
  59. {
  60. Alert.show("The XSLT document is malformed.");
  61. doProcess = false;
  62. }
  63. }
  64.  
  65. if (doProcess)
  66. {
  67. // Call into the JS to do the actual transformation.
  68. outputArea.text = html.window.transformXML(xml,xsl);
  69. }
  70. else
  71. {
  72. outputArea.text = null;
  73. }
  74. }
  75.  
  76. private var sampleXML:XML =
  77. <report-types>
  78. <report-type>Report 1</report-type>
  79. <report-type>Report 2</report-type>
  80. <report-type>Report 3</report-type>
  81. </report-types>;
  82.  
  83. private var sampleXSL:XML =
  84. <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  85. <xsl:template match="/report-types">
  86. <table>
  87. <tr>
  88. <th>Options</th>
  89. </tr>
  90. <tr>
  91. <xsl:for-each select="report-type">
  92. <td><xsl:value-of select="."/></td>
  93. </xsl:for-each>
  94. </tr>
  95. </table>
  96. </xsl:template>
  97. </xsl:stylesheet>;
  98. ]]>
  99. </mx:Script>
  100.  
  101. <mx:Label
  102. text ="XML to transform:"/>
  103.  
  104. <mx:TextArea
  105. id ="xmlArea"
  106. height ="30%"
  107. width ="100%"/>
  108.  
  109. <mx:Label
  110. text ="XSLT document:"/>
  111.  
  112. <mx:TextArea
  113. id ="xslArea"
  114. height ="35%"
  115. width ="100%"/>
  116.  
  117. <mx:Label
  118. text ="Output of transformation:"/>
  119.  
  120. <mx:TextArea
  121. id ="outputArea"
  122. text =""
  123. height ="30%"
  124. width ="100%"/>
  125.  
  126. <mx:HBox
  127. width ="100%"
  128. horizontalAlign ="center">
  129.  
  130. <mx:Button
  131. id ="button"
  132. label ="Process"
  133. click ="process()"/>
  134.  
  135. </mx:HBox>
  136.  
  137. </mx:WindowedApplication>

Report this snippet  

You need to login to post a comment.