The xsl file transforms it into HTML. I can't open the file in IE just fine and view the expected results. I would like to via a PowerShell script convert this in plain HTML. How would I go about that?

xsl文件将其转换为HTML。我无法在IE中打开文件,并查看预期的结果。我想通过PowerShell脚本将其转换为纯HTML。我该怎么办呢?

3 个解决方案

#1


5  

This blog entry has a code snippet that should work. It uses the System.Xml.Xsl.XslCompiledTransform .NET class to do the XSL transformation. The rest is just for getting the input and displaying the output.

此博客条目有一个应该有效的代码片段。它使用System.Xml.Xsl.XslCompiledTransform .NET类来执行XSL转换。其余的只是用于获取输入和显示输出。

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.

这本来是一个评论,但我想我会把它作为答案,这样对于寻找解决方案的其他人来说更容易。

#2


2  

The PowerShell Community Extensions has a Convert-Xml that will do an XSL transform on the XML. If the resulting file isn't valid HTML then you need to work on the XSL file.

PowerShell社区扩展有一个Convert-Xml,它将对XML进行XSL转换。如果生成的文件不是HTML,那么您需要处理XSL文件。

#3


0  

If you want to return an xml object instead of writing the output to file so you can do more in-line activities. 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.

如果要返回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
}

#1


5  

This blog entry has a code snippet that should work. It uses the System.Xml.Xsl.XslCompiledTransform .NET class to do the XSL transformation. The rest is just for getting the input and displaying the output.

此博客条目有一个应该有效的代码片段。它使用System.Xml.Xsl.XslCompiledTransform .NET类来执行XSL转换。其余的只是用于获取输入和显示输出。

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.

这本来是一个评论,但我想我会把它作为答案,这样对于寻找解决方案的其他人来说更容易。

#2


2  

The PowerShell Community Extensions has a Convert-Xml that will do an XSL transform on the XML. If the resulting file isn't valid HTML then you need to work on the XSL file.

PowerShell社区扩展有一个Convert-Xml,它将对XML进行XSL转换。如果生成的文件不是HTML,那么您需要处理XSL文件。

#3


0  

If you want to return an xml object instead of writing the output to file so you can do more in-line activities. 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.

如果要返回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
}
标签:

相关文章