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"
	exit;
}

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

$xslt = New-Object System.Xml.Xsl.XslCompiledTransform;
$xslt.Load($xsl);
$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
About these ads

4 Responses to Command-line XSLT processor with PowerShell

  1. Keith Hill says:

    FYI, this little bit of code is in cmdlet form in the PowerShell Community Extensions (Convert-Xml): http://pscx.codeplex.com. Along with the handy Format-Xml (and Format-Hex – think od, er octal dump).

  2. [...] By the way, the PowerShell version of this code snippet is here. [...]

  3. [...] xslt command is a translation of the PowerShell xslt script I posted some time [...]

  4. [...] a file is to write a little Powershell script to handle it. I found this on our WordPress neighbour Devioblog. There are extensions available to do this too, but I’m not in the mood to deviate too far [...]

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 66 other followers

%d bloggers like this: