Skip to content

Instantly share code, notes, and snippets.

@dkontyko
Forked from ericlaw1979/MakeItHTML.js
Last active February 21, 2024 15:08
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dkontyko/dd0c8a6f46068231a7c03dc66794d553 to your computer and use it in GitHub Desktop.
Save dkontyko/dd0c8a6f46068231a7c03dc66794d553 to your computer and use it in GitHub Desktop.
Transform XML+XSLT to plain html so that it loads without blocking (see https://textslashplain.com/2019/10/09/navigating-to-file-urls/ )
<#
.SYNOPSIS
Converts an XML file with an XSLT stylesheet to HTML for viewing in modern browsers.
.DESCRIPTION
Ported from https://gist.github.com/ericlaw1979/deb716d8436890420c41c8a593bfd509
to PowerShell for ease of use in environments where Node.js or DevTools are not available.
The resulting HTML file is saved to the same location as $XmlPath, with the file extension
changed to .html.
This can be used for DISA STIGs and IIS Failed Request Traces, among others.
.PARAMETER XmlPath
Path to the XML file to convert.
.PARAMETER XsltPath
Path to the XSLT stylesheet associated with the XML file.
.PARAMETER UnsafeEnableXsltScript
Enable XSLT script execution. This is disabled by default for security reasons.
Enabling this is necessary, for example, for IIS Failed Request Traces.
DO NOT enable this unless you trust the source of all files you are converting.
See https://learn.microsoft.com/en-us/previous-versions/windows/desktop/ms754621(v=vs.85)
for more information.
#>
param(
[Parameter(Mandatory=$true)]
[ValidateScript({Test-Path $_ -PathType 'Leaf'})]
[string]$XmlPath,
[Parameter(Mandatory=$true)]
[ValidateScript({Test-Path $_ -PathType 'Leaf'})]
[string]$XsltPath,
[Parameter()]
[switch]$UnsafeEnableXsltScript
)
function loadDomContent([string]$filePath) {
$rawContent = Get-Content -Path $filePath -Raw
$domContent = New-Object -ComObject "Msxml2.DOMDocument.6.0"
$domContent.validateOnParse = $true
$domContent.async = $false
$domContent.loadXML($rawContent) | Out-Null
return $domContent
}
$xmlDom = loadDomContent "$XmlPath"
$xsltDom = loadDomContent "$XsltPath"
if($UnsafeEnableXsltScript) {
$xsltDom.setProperty("AllowXsltScript", $true)
}
$htmlOutput = $xmlDom.transformNode($xsltDom)
$htmlOutput | Out-File -FilePath ([System.IO.Path]::ChangeExtension($XmlPath, "html"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment