Skip to content

Instantly share code, notes, and snippets.

@amccool
Created January 18, 2017 01:36
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save amccool/560b533f1ea94560e0dae6367c2b75ce to your computer and use it in GitHub Desktop.
Save amccool/560b533f1ea94560e0dae6367c2b75ce to your computer and use it in GitHub Desktop.
transform XML using XSLT
<#
.SYNOPSIS
Transform an xml file
.DESCRIPTION
load a XML file and load a XSLT file, then transform the XML using the XSLT and output to the console
.PARAMETER $xsltfilename
The path to the XSLT file
.PARAMETER $filename
The path to the XML file to be transformed
.EXAMPLE
C:\PS>
.\tranform.ps1 .\rgs_broke.xml .\fix_escalation.xslt > rgs_fixed.xml
.NOTES
Author: Alex McCool
Date: Jan 17, 2017
#>
param(
[Parameter(Mandatory=$true)]
[string]$xsltfilename,
[Parameter(Mandatory=$true)]
[string]$filename
)
function Load-Xml
{
param([string]$filename)
$content = Get-Content $filename
$stream = new-object System.IO.MemoryStream
$writer = new-object System.IO.StreamWriter($stream)
$writer.Write("$content")
$writer.Flush()
$stream.position = 0
$xml = new-object System.Xml.XmlTextReader($stream)
return $xml
}
function Load-Xslt
{
param([string]$filename)
$content = Get-Content $filename
$stream = new-object System.IO.MemoryStream
$writer = new-object System.IO.StreamWriter($stream)
$writer.Write("$content")
$writer.Flush()
$stream.position = 0
$reader = [System.Xml.XmlReader]::create($stream)
$xslt = New-Object System.Xml.Xsl.XslCompiledTransform
$xslt.Load($reader)
return $xslt
}
$xml = Load-Xml($filename)
$xslt = Load-Xslt($xsltfilename)
$output = New-Object System.IO.MemoryStream
$reader = new-object System.IO.StreamReader($output)
$arglist = new-object System.Xml.Xsl.XsltArgumentList
$xslt.Transform($xml, $arglist, $output)
$output.position = 0
$transformed = [string]$reader.ReadToEnd()
$reader.Close()
write-output $transformed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment