Skip to content

Instantly share code, notes, and snippets.

@ivanrad
Created June 30, 2017 14:33
Show Gist options
  • Select an option

  • Save ivanrad/092f4b85f1051297c80c194b79267246 to your computer and use it in GitHub Desktop.

Select an option

Save ivanrad/092f4b85f1051297c80c194b79267246 to your computer and use it in GitHub Desktop.
XSLT transform (wsh)
// @(#) xslt_transform.js
//
// (c) Ivan Radanovic <ivan.radanovic at gmail>
//
// usage: cscript /nologo xslt_transform.js <xslt_file> < <xml_file>
//
var xml, xslt;
function die(msg) {
WScript.StdErr.WriteLine(msg);
WScript.Quit(1);
}
// main
if (WScript.Arguments.Length != 1) // check if stylesheet arg has been supplied
die("usage: " + WScript.ScriptName + " <xslt_file>");
//
try {
var input="";
while (!WScript.StdIn.atEndOfStream)
input += "\n" + WScript.StdIn.ReadLine();
input = input.replace(/\s*\<\?.*?\?\>/gi, "\n\n\n");
// load xml file
xml = new ActiveXObject("Msxml2.DOMDocument.3.0");
xml.validateOnParse = true;
xmlasync = false;
xml.loadXML(input);
if (xml.parseError.errorCode != 0)
WScript.Quit(1);
// load xslt file
xslt = new ActiveXObject("Msxml2.DOMDocument.3.0");
xslt.validateOnParse = true;
xslt.async = false;
xslt.load(WScript.Arguments(0));
if (xslt.parseError.errorCode != 0)
WScript.Quit(1);
var output = WScript.CreateObject("ADODB.Stream");
output.Open();
output.Type = 2; // text
// output.Charset = "UTF-8"; // utf-8 encoding
xml.transformNodeToObject(xslt, output);
output.Position = 0;
var text=output.ReadText();
WScript.StdOut.WriteLine(text); // or output.SaveToFile("outputfile.txt")
output.Close();
WScript.Quit(0);
} catch (e) {
die("error: " + e.message);
WScript.Quit(1);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment