Search code examples
xmlpowershellxsltpscx

What is the syntax for converting XML to HTML using PSCX Convert-XML?


There is a commandlet in PSCX called Convert-XML that uses XSL to convert XML into HTML. I can't for the life of me figure out the syntax to get it to work.

I know I need to feed it paths for the XML input, the XSLT style sheet, and the HTML output.

How would one convert C:\input.xml into C:\output.html using C:\style.xslt?


Solution

  • Went back and forth in the question comments a bit, but I'm certain this is the correct answer:

    Convert-Xml -Path input.xml -XsltPath style.xslt -OutputPath output.html
    

    However, as you noted, you are on 2.1 of PSCX, which is fairly out of date and doesn't seem to support the -OutputFile parameter. You can try using the Out-File cmdlet or redirection operator to output the rendered HTML document to a file like so:

    # Using Out-File
    Convert-Xml -Path input.xml -XsltPath style.xslt | Out-File output.html
    
    # Using redirection operator (>)
    Convert-Xml -Path input.xml -XsltPath style.xslt > output.html
    

    Note that both Out-File and > adds a byte-order mark (BOM) to your file. If that is undesirable, you can do this to write the file without the BOM:

    # Save the converted HTML to a variable
    $htmlDoc = Convert-Xml -Path input.xml -XsltPath style.xslt
    
    # Create a new UTF8 encoding with no BOM
    $UTF8EncodingNoBom = New-Object System.Text.UTF8Encoding( $False )
    
    # Write to file using the no-BOM encoding
    [System.IO.File]::WriteAllText( 'output.html', $htmlDoc, $UTF8EncodingNoBom )