home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 67 / IOPROG_67A.ISO / soft / Tools / mwsppv4.exe / TRANSFORM.SCRIPT < prev    next >
Encoding:
Text File  |  2002-11-22  |  2.8 KB  |  103 lines

  1. !!Script
  2. // Copyright ⌐ 2001 - Modelworks Software
  3.  
  4. /**
  5. @Tool: transform~an XML document using a stylesheet.
  6. Note that this script should be considered an example
  7. and may need modifications to do what you want. See
  8. Microsoft's documentation for infomation about the 
  9. MSXML2 object and related classes.
  10. @Paragraph: To use this script open a xml file and 
  11. then run this script. .
  12.  
  13. @EndTool: 
  14. @Summary: transform~an XML document using a stylesheet
  15. */
  16.  
  17.  
  18. function DoCommand()
  19. {
  20.     var editor = getActiveEditor();
  21.     if (editor)
  22.     {
  23.         var findData = editor.findFirst("xml-stylesheet", 0, 0, true, true, true);
  24.         if (findData && findData.found)
  25.         {
  26.             findData = editor.findFirst("href=\"([^\"]+)", findData.range.startLineIndex, findData.range.startCharIndex, true, true, true);
  27.             if (findData && findData.found)
  28.             {
  29.                 var xslFile = editor.copy(findData.getSubRange(1));
  30.                 
  31.                 if (xslFile.length > 0)
  32.                 {
  33.                     var xmldoc = createObject("MSXML2.FreeThreadedDOMDocument");
  34.                     var xsldoc = createObject("MSXML2.FreeThreadedDOMDocument");
  35.                     
  36.                     if(xmldoc && xsldoc)
  37.                     {                        
  38.                         var lineCount = editor.getLineCount();
  39.                         var data = editor.copy(0,0,lineCount-1,32000); // copy all the data
  40.                         
  41.                         if (xmldoc.loadXML(data))
  42.                         {
  43.                             xsldoc.async = false;
  44.                             if (xsldoc.load(xslFile))
  45.                             {
  46.                             
  47.                                 var xsltemp = createObject("MSXML2.XSLTemplate");
  48.                                 xsltemp.stylesheet = xsldoc.documentElement;
  49.                                 var xslproc = xsltemp.createProcessor();
  50.                                 
  51.                                 xslproc.input = xmldoc.documentElement;                            
  52.                                 xslproc.transform();
  53.                                 
  54.                                 var display = newEditor("*." + GetFileType(editor), true);
  55.                                 display.append(xslproc.output);
  56.                             }
  57.                             else
  58.                             {
  59.                                 alert("load failed - you may one or more errors in your xsl file");
  60.                             }
  61.                         }
  62.                         else
  63.                         {
  64.                             alert("loadXML failed - you may one or more errors in your xml file");
  65.                         }
  66.                     }
  67.                     else
  68.                     {
  69.                         alert("Cannot create MSXML2.FreeThreadedDOMDocument");
  70.                     }
  71.                 }
  72.             }
  73.         }
  74.         else
  75.         {
  76.             alert("No xml-stylesheet element found in document");
  77.         }
  78.     }
  79. }
  80.  
  81. function GetFileType(editor)
  82. {
  83.     var findData = editor.findFirst("<xslt:output", 0, 0, true, true, false);
  84.     if (findData && findData.found)
  85.     {
  86.         findData = editor.findFirst("method=\"text/([^\"]+)", findData.range.startLineIndex, findData.range.startCharIndex, true, true, true);
  87.         if (findData && findData.found)
  88.         {
  89.             return editor.copy(findData.getSubRange(1));
  90.         }
  91.         findData = editor.findFirst("method=\"([^\"]+)", findData.range.startLineIndex, findData.range.startCharIndex, true, true, true);
  92.         if (findData && findData.found)
  93.         {
  94.             return editor.copy(findData.getSubRange(1));
  95.         }
  96.     }
  97.                                 
  98.     return "xml";
  99. }
  100.  
  101. !!/Script
  102.  
  103.