forked from RichardChmielek/Powershell.Xml2Html
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathXml2Html.ps1
49 lines (45 loc) · 1.59 KB
/
Xml2Html.ps1
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
using namespace System;
using namespace System.IO;
using namespace System.Xml;
using namespace System.Xml.Xsl;
if ($args.count -lt 3) {
Write-Error -Message "Required input files not found." -Category InvalidArgument
Write-Error "For Example: ./sample.ps1 ./sample.xml ./sample.xslt ./sample.html"
if (!$args[0]) {
Write-Error "Please pass input XML file path as first input!";
}
if (!$args[1]) {
Write-Error "Please pass XSLT file path as second input";
}
if (!$args[2]) {
Write-Error "Please pass output HTML file path as third input";
}
Exit
}
elseif ($args.count -eq 3) {
$xmlFile=$args[0];
$xsltFile=$args[1];
$htmlFile=$args[2];
#(Test-Path $args[0] -PathType leaf) && (Test-Path $args[1] -PathType leaf) && (Test-Path $args[2] -PathType leaf)
if((Test-Path $args[0] -PathType leaf) -And (Test-Path $args[1] -PathType leaf)){
Write-Host "All input files loaded successfully. Output HTML file will be created at the end.";
}
else{
Write-Error "Please check that all the input files exist at the given path!"
Exit
}
}
$transform = New-Object System.Xml.Xsl.XslCompiledTransform
Write-Host "Loading '$xsltFile' stylesheet..."
$xsltreader = [System.Xml.XmlReader]::Create($xsltFile);
try{
$transform.Load($xsltreader)
}
catch{
Write-Error "Failed to compile Stylesheet. Please check the contents."
Exit
}
Write-Host "Transforming '$xmlFile' to HTML Format..."
$transform.Transform($xmlFile,$htmlFile);
Write-Host "Html saved to '$htmlFile'" -ForegroundColor Green
Write-Host -ForegroundColor White