Newsletter

Executing and Debugging XSL Transformations in PowerShell

Unfortunaly the is no Cmdlet to execute XSL Transformations in PowerShell. During the development of the Xslt build step of the Crawler-Lib Build-Tools I had to deal with this. This is a small post about executing and debugging XSLT in PowerShell. 

Executing XSLT in PowerShell

The implementation of the XSLT build step shows how to pass parameters and activate debugging prior to the execution of the XSL Transformation:  

function Invoke-BuildStep_Xslt_Tool($context)
{
  $path = Expand-ParameterString $context.Step.Path
  $template = Expand-ParameterString $context.Step.Template
  $output = Expand-ParameterString $context.Step.Output
  if( ! (test-path $path )) { Throw"XML input file not found: $path"}
  $path = resolve-path $path 
  if( ! (test-path $template )) { Throw"XSL template file not found: $template"}
  $template = resolve-path $template 
  $output = [System.IO.Path]::GetFullPath([System.IO.Path]::Combine((Get-Location), $output))
  if( [System.Diagnostics.Debugger]::IsAttached )
  {
    $xslt = New-Object System.Xml.Xsl.XslCompiledTransform( $true )
  }
  else
  {
    $xslt = New-Object System.Xml.Xsl.XslCompiledTransform( $false )
  }
  $arglist = new-object System.Xml.Xsl.XsltArgumentList
  $arglist.AddParam("Config", "", $context.Config)
  $arglist.AddParam("CurrentBuild", "", $context.CurrentBuild)
  $arglist.AddParam("BuildCreatedUtc", "", $context.BuildCreatedUtc)
  $arglist.AddParam("BuildCreatedLocal", "", $context.BuildCreatedLocal)
  $arglist.AddParam("BuildSequenceName", "", $context.BuildSequenceName)
  $arglist.AddParam("Step", "", $context.Step)
  $arglist.AddParam("StepTool", "", $context.StepTool)
  $arglist.AddParam("StepNumber", "", $context.StepNumber)
  $arglist.AddParam("Version", "", $context.Version)
  $arglist.AddParam("FileVersion", "", $context.FileVersion)
  $arglist.AddParam("ProductVersion", "", $context.ProductVersion)
  $arglist.AddParam("PackageVersion", "", $context.PackageVersion)
  foreach( $param in $context.Step.Param )
  {
    $paramName = Expand-ParameterString $param.Name
    $paramNamespaceUri = Expand-ParameterString $param.NamespaceUri
    $paramValue = Expand-ParameterString $param.Value
    $arglist.AddParam($paramName, $paramNamespaceUri, $paramValue)
  }
  $xsltSettings = New-Object System.Xml.Xsl.XsltSettings($false,$true)
  $xslt.Load($template, $xsltSettings, (New-Object System.Xml.XmlUrlResolver))
  $outFile = New-Object System.IO.FileStream($output, [System.IO.FileMode]::Create, [System.IO.FileAccess]::Write)
  $xslt.Transform($path, $arglist, $outFile)
  $outFile.Close()
}

Debugging XSLT in PowerShell

Debugging of XSLT in PowerShell is possible with Visual Studio. The trick is to attach the PowerShell ISE or the PowerShell Command Processor to the Visual Studio debugger. This is simply be done by “Attach to Process …” under the “Debug” menu. After that the XSLT file must be loaded in Visual Studio. Then breakpoints can be added. After that preparation the PowerShell script and functions can be executed. When the  $xslt.Transform method is called the Visual Studio debugger will break on every breakpoint in the XSLT file.

 

Leave your comment