如果您想要返回一个xml对象,而不是将输出写入文件,那么您可以执行更多的内嵌活动。这将工作。另外我将处理器的创建分割成单独的功能,以便您可以创建一次并重新使用,这对内存更友好。
function Invoke-TransformXML($path,$styleSheetPath,$output,$parameters, $compiledtransform)
{
if(! (test-path $path)) { Throw"XML input file not found: $path"}
$path = resolve-path $path
if (! (Test-Path $compiledtransform))
{
if(! ($compiledtransform.GetType() -eq [System.Xml.Xsl.XslCompiledTransform]))
{
$ctType = $compiledtransform.GetType() ;
Throw "Compiled transform is wrong type: $ctType"
}
else
{
$xslt = $compiledtransform
}
}
if (($compiledtransform -eq $null))
{
if(! (test-path $styleSheetPath) ) { Throw"XSL template file not found: $styleSheetPath"}
$styleSheetPath = Resolve-Path $styleSheetPath
$xslt = Get-CompiledTransform $styleSheetPath
}
$transformed = New-Object System.IO.MemoryStream
try
{
$xslt.Transform([string]$path, [System.Xml.Xsl.XsltArgumentList]$arglist, [System.IO.Stream]$transformed)
$transformed.Position = 0
#$reader = New-Object System.Xml.XmlTextReader($ms)
$outdoc = New-Object System.Xml.XmlDocument
$outdoc.Load($transformed)
# close stream, we are done with it
$transformed.Close()
return $outdoc
} Finally {
$transformed.Close()
}
}
function Get-CompiledTransform($styleSheetPath)
{
if(! (test-path $styleSheetPath) ) { Throw"XSL template file not found: $styleSheetPath"}
$styleSheetPath = Resolve-Path $styleSheetPath
if([System.Diagnostics.Debugger]::IsAttached)
{
$xslt = New-Object System.Xml.Xsl.XslCompiledTransform($true)
}
else
{
$xslt = New-Object System.Xml.Xsl.XslCompiledTransform($false)
}
$arglist = new-object System.Xml.Xsl.XsltArgumentList
foreach($param in $parms)
{
if ($parms.Name)
{
$paramName = $parms.Name
$paramNamespaceUri = $parms.NamespaceUri
$paramValue = $parms.Value
$arglist.AddParam($paramName, $paramNamespaceUri, $paramValue)
}
}
$xsltSettings = New-Object System.Xml.Xsl.XsltSettings($false,$true)
$xslt.Load($styleSheetPath, $xsltSettings, (New-Object System.Xml.XmlUrlResolver))
return $xslt
}
http://devio.wordpress.com/2009/09/15/command-line-xslt-processor-with-powershell/ – austin 2013-04-08 21:09:13
有了一些解释,上面的评论应该是一个答案;可能是接受的答案(因为它不依赖于加载额外的模块)。 – alroc 2013-04-09 01:58:51