简体   繁体   English

使用PowerShell将XML转换为HTML

[英]Using PowerShell to convert XML to HTML

I have an XML file: 我有一个XML文件:

<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type='text/xsl' href='file:///C:/Program%20Files/Application/log_format.xsl'?>
<!DOCTYPE log [<!ENTITY data SYSTEM 'data/20130408.dat'>]>
<log xmlns="runtime:log">&data;</log>

The xsl file transforms it into HTML. xsl文件将其转换为HTML。 I can't open the file in IE just fine and view the expected results. 我无法在IE中打开文件,并查看预期的结果。 I would like to via a PowerShell script convert this in plain HTML. 我想通过PowerShell脚本将其转换为纯HTML。 How would I go about that? 我该怎么办呢?

This blog entry has a code snippet that should work. 博客条目有一个应该有效的代码片段。 It uses the System.Xml.Xsl.XslCompiledTransform .NET class to do the XSL transformation. 它使用System.Xml.Xsl.XslCompiledTransform .NET类来执行XSL转换。 The rest is just for getting the input and displaying the output. 其余的只是用于获取输入和显示输出。

This was originally a comment, but I guess I'll make it an answer so it's easier for other people who are searching for a solution. 这本来是一个评论,但我想我会把它作为答案,这样对于寻找解决方案的其他人来说更容易。

The PowerShell Community Extensions has a Convert-Xml that will do an XSL transform on the XML. PowerShell社区扩展有一个Convert-Xml ,它将对XML进行XSL转换。 If the resulting file isn't valid HTML then you need to work on the XSL file. 如果生成的文件不是HTML,那么您需要处理XSL文件。

If you want to return an xml object instead of writing the output to file so you can do more in-line activities. 如果要返回xml对象而不是将输出写入文件,以便可以执行更多内联活动。 this will work. 这会奏效。 in addition i split the creation of the processor into a separate function so that you can create it once and re-use which is more memory friendly. 此外,我将处理器的创建拆分为一个单独的功能,以便您可以创建一次并重新使用,这对内存更友好。

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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM