Project

Profile

Help

How to capture xsl:message output using Saxon XSLT transformer in PowerShell

Added by Eric Dupuis over 9 years ago

I cannot find a way to capture the 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.

Tried

1.Transformer.MessageListener. Problem: PowerShell doesn't support creating an interface to capture MessageListener.Message events 2.Transformer.TraceFunctionDestination. Problem: Can't instantiate StandardLogger with a constructor. $outLogger = New-Object Saxon.Api.StandardLogger -ArgumentList $outLogger $outLogger.UnderylingTextWriter = $outLogFile $saxTransform.TraceFunctionDestination = $outLogger 3.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:messageI 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()


Replies (1)

RE: How to capture xsl:message output using Saxon XSLT transformer in PowerShell - Added by O'Neil Delpratt over 9 years ago

Hi,

We have some sympathy with your requirement. But the use of the XslTransformer.MessageListener is the only why of intercepting where the xsl:message output is sent. I am not an expert in Powershell, but as a workaround I would think you can write an implementation of IMessageListener in C# and build this as an assembly for use within Powershell.

    (1-1/1)

    Please register to reply