by Patrice Krakow (July 19, 2000)
Download the source code for this article
When I want to test my XSL(T) scripts, I was always frustrated not to have a simple command-line utility which takes as arguments my XML source file, my XSL(T) script file and possibly some XSL(T) arguments.
Thus, I write it in JScript for Windows Scripting Host and here is the code:
// Name: Xslproc.js (Windows Script Host) // Copyright (C) 2000 Patrice Krakow // Author: Patrice Krakow (krakowpat@yahoo.com) // Date: July 2000 // Version 1.0 - Initial version (July 2000) // Purpose: Xslproc is a simple XSL(T) processor command-line utility which // uses MSXML3.DLL from Microsoft written in JavaScript for Windows // Scripting Host (WSH). main(); // // main // function main() { // Parse the parameters /////////////////////////////////////////////////// var oArgs = WScript.Arguments; var i, iStart; var strXmlFile; var strXslFile; var strOutputFile; var aParam = new Array(); var aValue = new Array(); if ( 2 > oArgs.Count() ) { var strMsg = "Xslproc is a simple XSL(T) processor command-line utility " + "which use MSXML3.DLL,\n" + "written in JavaScript for WSH by Patrice Krakow " + "(krakowpat@yahoo.com).\n\n" + "usage: xslproc source stylesheet [result] [param=value]..."; WScript.Echo(strMsg); WScript.Quit(); } strXMLFile = oArgs(0); strXSLFile = oArgs(1); if ( 2 == oArgs.Count() ) strOutputFile = ""; else if ( 3 <= oArgs.Count() ) { // Check if the third parameter is a file name or parameter: if ( -1 == oArgs(2).indexOf("=") ) { iStart = 3; strOutputFile = oArgs(2); } else { iStart = 2; strOutputFile = ""; } var iEqPos; for(i = iStart; i < oArgs.Count(); i++) { iEqPos = oArgs(i).indexOf("="); aParam[i-iStart] = oArgs(i).substring(0, iEqPos); aValue[i-iStart] = oArgs(i).substring(iEqPos+1, oArgs(i).length); } } // Process the XML by the XSLT script ///////////////////////////////////// // Create two DOM objects for respectively the XML and XSLT files: var oXMLDoc = new ActiveXObject("MSXML2.FreeThreadedDOMDocument"); var oXSLDoc = new ActiveXObject("MSXML2.FreeThreadedDOMDocument"); // Load the XML file: oXMLDoc.async = false; oXMLDoc.load(strXMLFile); // Check error: if ( 0 != oXMLDoc.parseError.errorCode ) { WScript.Echo("Error: The XML file is invalid."); WScript.Quit(); } // Load the XSL file: oXSLDoc.async = false; oXSLDoc.load(strXSLFile); // Check error: if ( 0 != oXSLDoc.parseError.errorCode ) { WScript.Echo("Error: The XSL file is invalid."); WScript.Quit(); } var oXSLTemplate = new ActiveXObject("MSXML2.XSLTemplate"); oXSLTemplate.stylesheet = oXSLDoc; var oProcessor = oXSLTemplate.createProcessor(); oProcessor.input = oXMLDoc; for(i = 0; i < aParam.length; i++) { oProcessor.addParameter(aParam[i], aValue[i]); } oProcessor.transform(); // Dump the result into the output file or to the standard output ///////// if ( "" == strOutputFile ) WScript.Echo(oProcessor.output); else { // Create the 'FileSystemObject' object: var fso = new ActiveXObject("Scripting.FileSystemObject"); // Create the output file or overwrite it if it already exists: var oFile = fso.CreateTextFile(strOutputFile, true /* override */); // Write the ouptut of the XSLT processing into the output file: oFile.Write(oProcessor.output); oFile.Close(); } }
This is the first draft version, I have to complete the error handling and to test it a little bit more, but it works pretty well to quickly test my XSL(T) scripts. I hope this can be useful for others and please feel free to improve it. To use this code, you should have installed the Windows Scripting Host which can be found at
http://msdn.microsoft.com/scripting/windowshost/download/default.htm
and the the May 2000 MSXML Technology Preview Release which can be found at
http://msdn.microsoft.com/downloads/webtechnology/xml/msxml.asp