Advertisement
rplantiko

XSLT transform with MSXML

Aug 20th, 2012
569
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // transform.js
  2. //
  3. // Analyzing why an MSXML XSLT transformation fails
  4. // Call the script with the filename (without extension) as arg
  5. //
  6. // Start in a cmd shell or in a batch file with:
  7. //
  8. // cscript //e:jscript transform.js name
  9. //
  10. // Style sheet and XML doc have this filename,
  11. // but different extensions (name.xsl and name.xml)
  12.  
  13. try {
  14.   WScript.StdOut.Write( transform( WScript.Arguments(0) ) );
  15.   } catch (e) {
  16.       WScript.Echo(e.message);
  17.       }
  18.  
  19. function transform(fileName) {
  20.   var xmlDocument   = get_xmlDocument(   fileName + ".xml" );
  21.   var xsltProcessor = get_xsltProcessor( fileName + ".xsl" );
  22.  
  23.   xsltProcessor.input = xmlDocument;
  24.   xsltProcessor.transform();
  25.   return xsltProcessor.output;    
  26.   }
  27.  
  28. function get_xmlDocument( xmlfile ) {
  29.   var xmlDocument = new ActiveXObject("Msxml2.DOMDocument.6.0");  
  30.   //                                or Msxml2.DOMDocument.3.0
  31.   xmlDocument.async = false;
  32.   xmlDocument.load( xmlfile );
  33.   if (xmlDocument.parseError.errorCode != 0) {
  34.      throw new Error( src.message );
  35.      }  
  36.   return xmlDocument;
  37.   }
  38.  
  39. function get_xslDocument( xslfile ) {  
  40.   var xslDocument = new ActiveXObject("Msxml2.FreeThreadedDOMDocument.6.0");  
  41. //                                  or Msxml2.FreeThreadedDOMDocument.3.0
  42.   xslDocument.async = false;
  43.   xslDocument.load( xslfile );
  44.   if (xslDocument.parseError.errorCode != 0) {
  45.      var myErr = xslDocument.parseError;
  46.      throw new Error("XSL parse error: " + myErr.reason);
  47.     }
  48.   return xslDocument;    
  49.   }
  50.  
  51. function get_xsltProcessor( xslfile ) {
  52.   var xslDocument = get_xslDocument( xslfile );
  53.   var xmlTemplate = new ActiveXObject( "Msxml2.XSLTemplate.6.0" );
  54. //                                   or Msxml2.XSLTemplate.3.0
  55.   xmlTemplate.stylesheet = xslDocument;
  56.   var xsltProcessor = xmlTemplate.createProcessor();
  57.   return xsltProcessor;
  58.   }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement