4

I have an XML file:

<?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. 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?

2
  • 1
    devio.wordpress.com/2009/09/15/… Commented Apr 8, 2013 at 21:09
  • 1
    With some explanation, the above comment should be an answer; possibly the accepted answer (since it doesn't depend upon loading an extra module). Commented Apr 9, 2013 at 1:58

3 Answers 3

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.

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.

Sign up to request clarification or add additional context in comments.

1 Comment

perfect solution, no code, just pointing to the right direction :)
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.

2 Comments

The link does not exist anymore
The module can be obtained here: powershellgallery.com/packages/Pscx
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.

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
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.