Skip to content

Instantly share code, notes, and snippets.

@wschwarz
Last active January 6, 2022 21:25
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wschwarz/5073004 to your computer and use it in GitHub Desktop.
Save wschwarz/5073004 to your computer and use it in GitHub Desktop.
Powershell script to run an xslt transform on a passed in file.
function process-XSLT
{param([string]$a)
$xsl = "C:\path_to_xslt\CleanUp.xslt"
$inputstream = new-object System.IO.MemoryStream
$xmlvar = new-object System.IO.StreamWriter($inputstream)
$xmlvar.Write("$a")
$xmlvar.Flush()
$inputstream.position = 0
$xml = new-object System.Xml.XmlTextReader($inputstream)
$output = New-Object System.IO.MemoryStream
$xslt = New-Object System.Xml.Xsl.XslCompiledTransform
$arglist = new-object System.Xml.Xsl.XsltArgumentList
$reader = new-object System.IO.StreamReader($output)
$xslt.Load($xsl)
$xslt.Transform($xml, $arglist, $output)
$output.position = 0
$transformed = [string]$reader.ReadToEnd()
$reader.Close()
return $transformed
}
@rlyders
Copy link

rlyders commented Mar 14, 2014

I'm trying your example, along with several others, under Windows 8.1 and I consistently get the following error. Has this call changed in the latest revision of code? I must have gone through about 10 various example code bodies and they all result in the exact same error it appears. I've Googled this error for hours and have found references noting that I can't use XSL 2.0 and other references to how the XSL might have includes or imports... but none of those apply to my simple example. Another post notes that it might be caused when running the example repeatedly as it should only be compiled once, but there wasn't any clear note regarding how to avoid compiling it more than once if the xslcompiledtransform object clearly has to be declared in order to be used. Any help would be appreciated. Apparently I am missing something basic here.

ERRORS:

Exception calling "Load" with "1" argument(s): "XSLT compile error."
At C:\Users\Jim\SkyDrive\Documents\ps1\xlst_transform.ps1:2 char:1

  • $xslt.load('C:\Users\Jim\SkyDrive\Documents\ps1\cdcatalog.xsl')
  • - CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    - FullyQualifiedErrorId : XslLoadException
    
    

Exception calling "Transform" with "2" argument(s): "No stylesheet was loaded."
At C:\Users\Jim\SkyDrive\Documents\ps1\xlst_transform.ps1:3 char:1

  • $xslt.Transform('C:\Users\Jim\SkyDrive\Documents\ps1\cdcatalog.xml', 'C:\Us ...
  • - CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    - FullyQualifiedErrorId : InvalidOperationException
    

@LarsJohansson
Copy link

Hi, Much appreciated function, worked spotlessly the first time.
I have embedded the function in a script which I intend to license under GNU General Public License. Is that ok with you? In the script I remarked I found the function here.

function transXSL{param([string]$myxml,[string]$myxsl)

This XSL transform function found at https://gist.github.com/wschwarz/5073004

...
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment