Last active
January 6, 2022 21:25
-
-
Save wschwarz/5073004 to your computer and use it in GitHub Desktop.
Powershell script to run an xslt transform on a passed in file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} |
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
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
Exception calling "Transform" with "2" argument(s): "No stylesheet was loaded."
At C:\Users\Jim\SkyDrive\Documents\ps1\xlst_transform.ps1:3 char:1