-
Notifications
You must be signed in to change notification settings - Fork 89
/
xslTransform.js
83 lines (67 loc) · 2.43 KB
/
xslTransform.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
var oArgs = WScript.Arguments;
if( oArgs.length == 0 )
{
WScript.StdErr.WriteLine( "What do you want me to transform?" );
printUsage();
WScript.Quit( -1 );
}
if( ("/?" == oArgs( 0 )) || ("-?" == oArgs( 0 )) )
{
printUsage();
WScript.Quit( -1 );
}
if( oArgs.length != 2 )
{
WScript.StdErr.WriteLine( "Bad parameters; expected two but got " + oArgs.length + "." );
printUsage();
WScript.Quit( -1 );
}
xmlFile = oArgs( 0 );
xslFile = oArgs( 1 );
// Breaking right here? Don't have MSXML6? Get it from:
// http://www.microsoft.com/downloads/details.aspx?FamilyID=993C0BCF-3BCF-4009-BE21-27E85E1857B1&displaylang=en
// and
// http://www.microsoft.com/downloads/details.aspx?FamilyID=d21c292c-368b-4ce1-9dab-3e9827b70604&displaylang=en
var xsl = new ActiveXObject( "MSXML2.DOMDOCUMENT.6.0" );
var xml = new ActiveXObject( "MSXML2.DOMDocument.6.0" );
xml.validateOnParse = false;
xml.async = false;
xml.load( xmlFile );
if( xml.parseError.errorCode != 0 )
{
WScript.StdErr.WriteLine( "XML Parse Error : " + xml.parseError.reason );
WScript.StdErr.WriteLine( "error code " + xml.parseError.errorCode + " line " + xml.parseError.line + ":" + xml.parseError.linepos );
WScript.Quit( xml.parseError.errorCode );
}
xsl.async = false;
xsl.load( xslFile );
xsl.setProperty( "AllowXsltScript", true );
if( xsl.parseError.errorCode != 0 )
{
WScript.StdErr.WriteLine( "XSL Parse Error : " + xsl.parseError.reason );
WScript.StdErr.WriteLine( "error code " + xsl.parseError.errorCode + " line " + xsl.parseError.line + ":" + xsl.parseError.linepos );
WScript.Quit( xsl.parseError.errorCode );
}
try
{
WScript.Echo( xml.transformNode( xsl.documentElement ) );
}
catch( err )
{
WScript.StdErr.WriteLine( "Transformation Error : " + numberToHexString( err.number ) + ": " + err.description );
WScript.Quit( err.number );
}
// Wow; printing a number in hex is a little more complicated than I would expect.
function numberToHexString( num )
{
return "0x" + ((num >> 16) & 0x0000FFFF).toString(16) + (num & 0x0000FFFF).toString(16);
} // end of numberToHexString()
function printUsage()
{
WScript.Echo( "" );
WScript.Echo( "Usage: cscript xslTransform.js xmlFile xslFile" );
WScript.Echo( "" );
WScript.Echo( " The transformed contents of xmlFile are sent to stdout." );
WScript.Echo( " The AllowXsltScript property is set to true, which has security implications. Do not run this script on untrusted input." );
WScript.Echo( "" );
} // end printUsage()