Convert-Xml with XSLT in PowerShell
18
Dec
Just a quick post to upload a simple PowerShell script that transforms XML files with XSL files. There’s actually a cmdlet in PowerShell Community Extensions which does this, but believe it or not, in all my tests the script outperforms the cmdlet (the cmdlet takes, on average, 117ms on a file which takes the script 63ms on average).
function Convert-WithXslt($originalXmlFilePath, $xslFilePath, $outputFilePath)
{
## Simplistic error handling
$xslFilePath = resolve-path $xslFilePath
if( -not (test-path $xslFilePath) ) { throw "Can't find the XSL file" }
$originalXmlFilePath = resolve-path $originalXmlFilePath
if( -not (test-path $originalXmlFilePath) ) { throw "Can't find the XML file" }
$outputFilePath = resolve-path $outputFilePath
if( -not (test-path (split-path $originalXmlFilePath)) ) { throw "Can't find the output folder" }
## Get an XSL Transform object (try for the new .Net 3.5 version first)
$EAP = $ErrorActionPreference
$ErrorActionPreference = "SilentlyContinue"
$script:xslt = new-object system.xml.xsl.xslcompiledtransfrm
trap [System.Management.Automation.PSArgumentException]
{ # no 3.5, use the slower 2.0 one
$ErrorActionPreference = $EAP
$script:xslt = new-object system.xml.xsl.xsltransform
}
$ErrorActionPreference = $EAP
## load xslt file
$xslt.load( $xslFilePath )
## transform
$xslt.Transform( $originalXmlFilePath, $outputFilePath )
}
- By Joel 'Jaykul' Bennett
- Tagged as:
Send a comment