Skip to content

Instantly share code, notes, and snippets.

@Scorpio59
Forked from sayedihashimi/transform-xml.ps1
Last active July 17, 2019 08:47
Show Gist options
  • Select an option

  • Save Scorpio59/8049318885f9cbb4dbc1243a69d654b8 to your computer and use it in GitHub Desktop.

Select an option

Save Scorpio59/8049318885f9cbb4dbc1243a69d654b8 to your computer and use it in GitHub Desktop.
Script which can be used to transform an XML file using XDT. All you need to do is download the script and call it. The script will download the files it needs to run.
<#
.SYNOPSIS
You can use this script to easly transform any XML file using XDT.
To use this script you can just save it locally and execute it. The script
will download it's dependencies automatically.
#>
[cmdletbinding()]
param(
[Parameter(Mandatory=$true,Position=0)]
$sourceFile,
[Parameter(Mandatory=$true,Position=1)]
$transformFile,
[Parameter(Mandatory=$true,Position=2)]
$destFile ,
$toolsDir = ("$env:LOCALAPPDATA\LigerShark\tools\"),
$nugetDownloadUrl = 'https://dist.nuget.org/win-x86-commandline/latest/nuget.exe'
)
<#
.SYNOPSIS
If nuget is not in the tools
folder then it will be downloaded there.
#>
function Get-Nuget(){
[cmdletbinding()]
param(
$toolsDir = ("$env:LOCALAPPDATA\LigerShark\tools\"),
$nugetDownloadUrl = 'https://dist.nuget.org/win-x86-commandline/latest/nuget.exe'
)
process{
$nugetDestPath = Join-Path -Path $toolsDir -ChildPath nuget.exe
if(!(Test-Path $nugetDestPath)){
'Downloading nuget.exe' | Write-Verbose
# download nuget
$webclient = New-Object System.Net.WebClient
$webclient.DownloadFile($nugetDownloadUrl, $nugetDestPath)
# double check that is was written to disk
if(!(Test-Path $nugetDestPath)){
throw 'unable to download nuget'
}
}
# return the path of the file
$nugetDestPath
}
}
function GetTransformXmlExe(){
[cmdletbinding()]
param(
$toolsDir = ("$env:LOCALAPPDATA\LigerShark\tools\")
)
process{
if(!(Test-Path $toolsDir)){
New-Item -Path $toolsDir -ItemType Directory | Out-Null
}
'Downloading xdt since it was not found in the tools folder' | Write-Verbose
# nuget install SlowCheetah.Xdt -Prerelease -OutputDirectory toolsDir\
$cmdArgs = @('install','Microsoft.Web.Xdt','-Version','2.1.2','-Source', 'https://api.nuget.org/v3/index.json','-OutputDirectory',(Resolve-Path $toolsDir).ToString())
&(Get-Nuget -toolsDir $toolsDir -nugetDownloadUrl $nugetDownloadUrl) $cmdArgs | Out-Null
$xdtExe = (Get-ChildItem -Path $toolsDir -Include 'SlowCheetah.Xdt.exe' -Recurse) | Select-Object -First 1
# copy the xdt assemlby if the xdt directory is missing it
# copy the xdt.dll next to the slowcheetah .exe
$xdtDll = (Get-ChildItem -Path $toolsDir -Include 'Microsoft.Web.XmlTransform.dll' -Recurse) | Select-Object -First 1
if(!$xdtDll){ throw 'Microsoft.Web.XmlTransform.dll not found' }
Add-Type -LiteralPath $xdtDll.Fullname
$xdtExe
}
}
function Transform-Xml{
[cmdletbinding()]
param(
[Parameter(
Mandatory=$true,
Position=0)]
$sourceFile,
[Parameter(
Mandatory=$true,
Position=1)]
$transformFile,
[Parameter(
Mandatory=$true,
Position=2)]
$destFile,
$toolsDir = ("$env:LOCALAPPDATA\LigerShark\tools\")
)
process{
# slowcheetah.xdt.exe <source file> <transform> <dest file>
$cmdArgs = @((Resolve-Path $sourceFile).ToString(),
(Resolve-Path $transformFile).ToString(),
$destFile)
'Calling slowcheetah.xdt.exe with the args: [{0} {1}]' -f (GetTransformXmlExe), ($cmdArgs -join ' ') | Write-Verbose
$xmldoc = New-Object "Microsoft.Web.XmlTransform.XmlTransformableDocument";
$xmldoc.PreserveWhitespace = $true
$xmldoc.Load($sourceFile);
$transf = New-Object Microsoft.Web.XmlTransform.XmlTransformation($transformFile);
if ($transf.Apply($xmldoc) -eq $false)
{
throw "Transformation failed."
}
$xmldoc.Save($destFile);
}
}
Transform-Xml -sourceFile $sourceFile -transformFile $transformFile -destFile $destFile
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment