0

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?

convoke
  • 191
  • 11
  • What have you tried already? Looking at the help for the cmdlet, looks like `Convert-Xml -Path input.xml -XsltPath style.xslt -OutputPath output.html` should work. – codewario Jul 12 '18 at 19:17
  • When I try that, I get an error: "A parameter cannot be found that matches parameter name 'OutputPath' – convoke Jul 12 '18 at 19:52
  • If I remove -OutputPath output.html it works, but just echos the HTML in powershell as opposed to writing it to a file. – convoke Jul 12 '18 at 19:56
  • `help convert-xml` shows an `-OutputPath` parameter for me. Which version of PSCX are you using? – codewario Jul 12 '18 at 19:58
  • 2.1, I think... – convoke Jul 12 '18 at 20:10
  • If you are running the command in powershell, could you just user Powershell's redirection operator? `Convert-Xml -Path input.xml -XsltPath style.xslt > output.html` – Tim C Jul 13 '18 at 08:21
  • Well, if the version you have doesn't support `-OutputPath`, what happens if you `Convert-Xml > file.html` or `Convert-Xml | Out-File file.html` (with the other arguments on `Convert-Xml` of course) – codewario Jul 13 '18 at 13:35
  • Alternatively can you update `pscx` to the latest version? I just updated mine and it is `3.3.2` – codewario Jul 13 '18 at 13:36

1 Answers1

1

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 )
codewario
  • 19,553
  • 20
  • 90
  • 159