Command-line XSLT processor with PowerShell

There are already a lot of XSLT processors out there, such as MSXSL, but without downloading and installing an application you can create your own processor using a couple of PowerShell lines and the System.Xml.Xsl namespace of .Net:

param ($xml, $xsl, $output)

if (-not $xml -or -not $xsl -or -not $output)
	Write-Host "& .\xslt.ps1 [-xml] xml-input [-xsl] xsl-input [-output] transform-output"

trap [Exception]
	Write-Host $_.Exception;

$xslt = New-Object System.Xml.Xsl.XslCompiledTransform;
$xslt.Transform($xml, $output);

Write-Host "generated" $output;

What does this code do:

  • Declare command-line parameters $xml, $xsl, $output
  • Check parameters are passed to the script
  • Set a trap to display detailed error message in case an exception is raised
  • Load the XSLT file
  • Transform XML and write result to output file

6 thoughts on “Command-line XSLT processor with PowerShell

  1. Pingback: XSLT Transformations on XML Files with Undeclared Namespaces « devioblog

  2. Pingback: tee, xslt « devioblog

  3. Pingback: Processing structured text data with XSLT « hypercene

  4. Pingback: XSLT with Powershell | Powershell Faq

  5. Exception calling “Transform” with “2” argument(s): “Name cannot begin with the ‘.’ character, hexadecimal value 0x00.
    Line 1, position 40.”
    At line:1 char:1
    + $xslt.Transform($xml, $output);

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.