I cannot find a way to capture the <xsl:message/>
output of the .NET Saxon XSLT transformer when running via PowerShell.
I've tried various PowerShell methods of capturing output and various ways to tell Saxon to output the data without success. I need to use the Saxon XSLT library because our company is committed to it.
Tried
Transformer.MessageListener
. Problem: PowerShell doesn't support creating an interface to captureMessageListener.Message
eventsTransformer.TraceFunctionDestination
. Problem: Can't instantiateStandardLogger
with a constructor.$outLogger = New-Object Saxon.Api.StandardLogger -ArgumentList $outLogger $outLogger.UnderylingTextWriter = $outLogFile $saxTransform.TraceFunctionDestination = $outLogger
Various PowerShell output redirection methods. I don't think Saxon plays nice with that.
$out = $saxTransform.Run($outSerializer) *> "c:\eric\log.txt" Write-Output "nada-> $out"
XSLT:
<xsl:template match="/">
<xsl:message>I want to see this in a file</xsl:message>
</xsl:template>
PowerShell:
# Instantiate an Xslt transformer
$saxonPath = GetSaxonDllPath
Add-Type -Path $saxonPath
$saxProcessor = New-Object Saxon.Api.Processor
$saxCompiler = $saxProcessor.NewXsltCompiler()
$baseXsltPath = [System.IO.Path]::GetDirectoryName($inXsltPath)
$uri = [System.Uri]$baseXsltPath
$saxCompiler.BaseUri = $uri
Write-Output "Compiling..."
$uri = [System.Uri]$inXsltPath
$saxExecutable = $saxCompiler.Compile($uri)
$saxTransform = $saxExecutable.Load()
#Prepare the input XML file
$basePath = [System.IO.Path]::GetDirectoryName($inXmlFilePath)
$uri = [System.Uri]$basePath
$inFileStream = [System.IO.File]::OpenRead($inXmlFilePath)
$saxTransform.SetInputStream($inFileStream, $uri);
##Set XSLT processing parameters
if ($arguments) {
foreach($argument in $arguments.GetEnumerator()) {
$paramName = New-Object Saxon.Api.QName($argument.Name)
$paramValue = New-Object Saxon.Api.XdmAtomicValue($argument.Value)
$saxTransform.SetParameter($paramName, $paramValue)
}
}
#Prepare the output file
$outFileStream = [System.IO.File]::OpenWrite($outFileName)
$outSerializer = New-Object Saxon.Api.Serializer
$outSerializer.SetOutputStream($outFileStream);
$outLogFile = [System.IO.File]::OpenWrite("c:\eric\log.txt")
$outLogger = New-Object Saxon.Api.StandardLogger($outLogFile)
#Transform
Write-Output "Transforming..."
$saxTransform.Run($outSerializer)
Write-Output "Transformation done."
$outLogFile.Close()
$outLogFile.Dispose()
#Cleanup
$inFileStream.Close()
$inFileStream.Dispose()
$outFileStream.Close()
$outFileStream.Dispose()