e-GovのXMLをHTMLに変換するVBS

e-GovのサイトからDLした通知書(公文書)がXMLファイル形式になっています。
<例、保険料納入告知額・領収済額通知書_令和7年9月送付分(202509~).xml>

Microsoft EdgeのバージョンアップでIEモードが有効にならなくなって、ブラウザでは表示できない状態になっています。以前はEdgeのIEモードでそのまま開けていましたが、最近のブラウザ環境ではうまく表示できず、「公文書を開いても真っ白になる」「文字列のみ表示される」など正常に表示されない場合が増えました。

そこで今回は、e-GovのXML公文書をドラッグ&ドロップでHTMLに一発変換するVBSスクリプトを作成したので、それを利用してIEモードがなくても開けるようにする方法をここにメモしておこう。


VBSスクリプトの概要

今回作成したVBSスクリプトは、次のような動きをします。

  1. XMLファイルをこのVBSファイルにドラッグ&ドロップする
  2. 同じフォルダに、XMLと同名のHTMLファイルを出力
  3. 出力されたHTMLにCSSを追加する(preタグに折り返しを設定)
  4. ブラウザで開けば、公文書の内容を読みやすい形で表示可能

つまり、「XML → HTML」の単純な変換を自動でやってくれる小さなツールになります。そのまま変換するだけだと、preタグの折り返し部分が無いので、枠から文字がはみ出してしまいます。なのでCSSも追加して書式を保つように工夫しています。HTMLなので、IEモードが無くても、どのブラウザでも開けるようになります。


動作環境

  • OS:Windows 10 / 11
  • 標準のWindowsスクリプトホスト(WScript)で動作
  • 追加のソフトウェアやインストールは不要

VBSはメモ帳1つで簡単に作れる自動化プログラムです。


VBSスクリプト本体

以下が、XMLをHTMLに変換してCSSを追加するVBSです。

VBSコード
'====================================================
' ConvertXMLtoHTML_UTF8fix_AutoWrap.vbs
' XML + XSL -> HTML (force UTF-8 with BOM; auto wrap including <pre>)
'====================================================

Option Explicit

Dim fso, args, i
Set fso = CreateObject("Scripting.FileSystemObject")
Set args = WScript.Arguments

If args.Count = 0 Then
    MsgBox "Drag and drop XML file(s) onto this script.", vbExclamation, "XML to HTML"
    WScript.Quit
End If

For i = 0 To args.Count - 1
    Call ConvertXmlToHtml(fso.GetAbsolutePathName(args(i)))
Next

MsgBox "Done. HTML created.", vbInformation, "XML to HTML"
WScript.Quit


'------------------------------
Sub ConvertXmlToHtml(xmlPath)
    Dim xslHref, xslPath, htmlPath, extPos
    Dim tpl, xslDoc, xmlDoc, proc
    Dim htmlText

    If Not fso.FileExists(xmlPath) Then
        MsgBox "XML file not found:" & vbCrLf & xmlPath, vbCritical, "XML to HTML"
        Exit Sub
    End If

    xslHref = GetXslHref(xmlPath)
    If xslHref = "" Then
        MsgBox "No xml-stylesheet href found in:" & vbCrLf & xmlPath, vbExclamation, "XML to HTML"
        Exit Sub
    End If

    xslPath = fso.BuildPath(fso.GetParentFolderName(xmlPath), xslHref)
    If Not fso.FileExists(xslPath) Then
        MsgBox "XSL file not found:" & vbCrLf & xslPath, vbCritical, "XML to HTML"
        Exit Sub
    End If

    extPos = InStrRev(xmlPath, ".")
    If extPos > 0 Then
        htmlPath = Left(xmlPath, extPos - 1) & ".html"
    Else
        htmlPath = xmlPath & ".html"
    End If

    ' --- create MSXML objects ---
    Set tpl   = CreateXslTemplate()
    Set xslDoc = CreateFreeThreadedDom()
    Set xmlDoc = CreateDom()
    If tpl Is Nothing Or xslDoc Is Nothing Or xmlDoc Is Nothing Then
        MsgBox "MSXML is not available on this system.", vbCritical, "XML to HTML"
        Exit Sub
    End If

    ' --- load XSL ---
    xslDoc.async = False
    xslDoc.resolveExternals = True
    xslDoc.validateOnParse = False
    If Not xslDoc.Load(xslPath) Then
        MsgBox "XSL load error: " & xslDoc.parseError.reason & vbCrLf & xslPath, vbCritical
        Exit Sub
    End If
    If xslDoc.parseError.errorCode <> 0 Then
        MsgBox "XSL parse error: " & xslDoc.parseError.reason & vbCrLf & xslPath, vbCritical
        Exit Sub
    End If
    Set tpl.stylesheet = xslDoc

    ' --- load XML ---
    xmlDoc.async = False
    xmlDoc.resolveExternals = True
    xmlDoc.validateOnParse = False
    If Not xmlDoc.Load(xmlPath) Then
        MsgBox "XML load error: " & xmlDoc.parseError.reason & vbCrLf & xmlPath, vbCritical
        Exit Sub
    End If
    If xmlDoc.parseError.errorCode <> 0 Then
        MsgBox "XML parse error: " & xmlDoc.parseError.reason & vbCrLf & xmlPath, vbCritical
        Exit Sub
    End If

    ' --- transform ---
    Set proc = tpl.createProcessor()
    proc.input = xmlDoc
    If Not proc.transform() Then
        MsgBox "XSLT transform failed." & vbCrLf & xmlPath, vbCritical
        Exit Sub
    End If

    ' --- postprocess: encoding + CSS auto wrap ---
    htmlText = CStr(proc.output)
    htmlText = Replace(htmlText, "charset=Shift_JIS", "charset=UTF-8")
    htmlText = Replace(htmlText, "charset=shift_jis", "charset=UTF-8")

    ' insert UTF-8 meta if missing
    If InStr(1, htmlText, "<meta charset=""UTF-8"">", 1) = 0 Then
        Dim posHead
        posHead = InStr(1, htmlText, "<head>", 1)
        If posHead > 0 Then
            htmlText = Left(htmlText, posHead + 5) & vbCrLf & "<meta charset=""UTF-8"">" & Mid(htmlText, posHead + 6)
        End If
    End If

    ' insert CSS for auto wrapping (including <pre>)
    If InStr(1, htmlText, "<style>", vbTextCompare) = 0 Then
        Dim headPos
        headPos = InStr(1, htmlText, "<head>", vbTextCompare)
        If headPos > 0 Then
            htmlText = Left(htmlText, headPos + 5) & vbCrLf & _
            "<style>" & vbCrLf & _
            "body, td, th, div, p, span {" & vbCrLf & _
            "  white-space: normal;" & vbCrLf & _
            "  word-wrap: break-word;" & vbCrLf & _
            "  overflow-wrap: anywhere;" & vbCrLf & _
            "}" & vbCrLf & _
            "pre {" & vbCrLf & _
            "  white-space: pre-wrap;" & vbCrLf & _
            "  word-break: break-all;" & vbCrLf & _
            "  overflow-wrap: anywhere;" & vbCrLf & _
            "}" & vbCrLf & _
            "</style>" & vbCrLf & _
            Mid(htmlText, headPos + 6)
        End If
    End If

    ' --- write as UTF-8 with BOM ---
    Dim s
    Set s = CreateObject("ADODB.Stream")
    s.Type = 2
    s.Charset = "UTF-8"
    s.Open
    s.WriteText htmlText
    s.SaveToFile htmlPath, 2
    s.Close
End Sub


'------------------------------
Function GetXslHref(xmlPath)
    Dim doc, n, data, rx, m
    GetXslHref = ""
    Set doc = CreateDom()
    If doc Is Nothing Then Exit Function
    doc.async = False
    doc.resolveExternals = False
    doc.validateOnParse = False
    If Not doc.Load(xmlPath) Then Exit Function
    For Each n In doc.childNodes
        If n.nodeType = 7 Then
            If LCase(n.target) = "xml-stylesheet" Then
                data = CStr(n.data)
                Set rx = CreateObject("VBScript.RegExp")
                rx.Pattern = "href\s*=\s*((""([^""]*)"")|'([^']*)")
                rx.IgnoreCase = True
                rx.Global = False
                If rx.Test(data) Then
                    Set m = rx.Execute(data)(0)
                    If m.SubMatches(1) <> "" Then
                        GetXslHref = m.SubMatches(1)
                    Else
                        GetXslHref = m.SubMatches(2)
                    End If
                    Exit Function
                End If
            End If
        End If
    Next
End Function


'------------------------------
Function CreateDom()
    On Error Resume Next
    Dim d
    Set d = CreateObject("Msxml2.DOMDocument.6.0")
    If Err.Number <> 0 Or (d Is Nothing) Then
        Err.Clear
        Set d = CreateObject("Msxml2.DOMDocument.3.0")
    End If
    On Error GoTo 0
    Set CreateDom = d
End Function

Function CreateFreeThreadedDom()
    On Error Resume Next
    Dim d
    Set d = CreateObject("Msxml2.FreeThreadedDOMDocument.6.0")
    If Err.Number <> 0 Or (d Is Nothing) Then
        Err.Clear
        Set d = CreateObject("Msxml2.FreeThreadedDOMDocument.3.0")
    End If
    On Error GoTo 0
    Set CreateFreeThreadedDom = d
End Function

Function CreateXslTemplate()
    On Error Resume Next
    Dim t
    Set t = CreateObject("Msxml2.XSLTemplate.6.0")
    If Err.Number <> 0 Or (t Is Nothing) Then
        Err.Clear
        Set t = CreateObject("Msxml2.XSLTemplate.3.0")
    End If
    On Error GoTo 0
    Set CreateXslTemplate = t
End Function

セキュリティ上の理由から、インターネットからダウンロードしたVBSファイルはブロックされる場合があります。なので、自分でメモ帳にコピー&ペーストして .vbs ファイルとして保存します。細かい作成手順は次の項目で説明します。


VBS作成手順

  1. このページのVBSスクリプトをメモ帳などにコピペする
  2. ファイル - 名前を付けて保存 をクリックする
  3. ファイル名を「ConvertXMLtoHTML_UTF8fix.vbs」等にする
  4. ファイル形式を「テキスト」から「すべて」に変更
  5. 文字コードは「UTF-8」にする
  6. デスクトップなどのわかりやすい場所に「保存」にする

これで、VBSスクリプトが完成しました。
(エラーが出る場合は手順が間違っています)


VBS利用手順

  1. e-Govのサイトから目的の公文書をXML形式でダウンロードする。
  2. ダウンロードしたXMLファイルを、
    ConvertXMLtoHTML_UTF8fix.vbs」にドラッグ&ドロップします。
  3. 同じフォルダに「元ファイル名.html」が生成される
  4. 生成されたHTMLファイルをブラウザ(EdgeやChromeなど)で開きます。

これで、IEモードに頼らなくても、公文書の内容を読みやすいHTMLとして開くことができます。

注意点・免責事項

  • 本スクリプトは、個人利用・業務効率化を目的として作成したものです。
  • e-Gov側の仕様変更や、XMLの形式が大きく変わった場合、うまく変換できない可能性があります。
  • 機密性の高い文書を扱う場合は、取り扱いや保存場所に十分ご注意ください。
  • 本スクリプトの利用によって生じたいかなるトラブルについても、当方では責任を負いかねます。

おわりに

e-Govの公文書は内容自体は非常に有用ですが、「XML形式であること」や「IEモードに依存していたこと」が原因で、ちょっと中身を確認したいだけでも手間がかかる場面がありました。

今回のVBSスクリプトを使えば、XMLファイルをドラッグ&ドロップするだけでHTML化できるので、日常的にe-Govから公文書をダウンロードしている方の作業負担が少しでも軽くなれば幸いです。

必要に応じて、ご自身の環境や用途に合わせてスクリプトをカスタマイズしてみてください。