Click here to Skip to main content
13,193,064 members (82,639 online)
Click here to Skip to main content
Add your own
alternative version


3 bookmarked
Posted 18 Aug 2014

Executing and Debugging XSL Transformations in PowerShell

Rate this:
Please Sign up or sign in to vote.
Executing and debugging XSL transformations in PowerShell

Unfortunately, there 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 )
    $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)

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.


This article, along with any associated source code and files, is licensed under A Public Domain dedication


About the Author

I'm working on a new project called Crawler-Lib. It is a generalized back-end processing and hosting framework for Microsoft .NET and Mono. Please take a look at it:
Crawler-Lib Homepage
Crawler-Lib Blog
Crawler-Lib YouTube Channel

You may also be interested in...

Comments and Discussions

-- There are no messages in this forum --
Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.171017.2 | Last Updated 18 Aug 2014
Article Copyright 2014 by Thomas Maierhofer (Tom)
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid