且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用XSLT转换XML

更新时间:2022-11-28 16:19:09

每当使用记录集调用方式从字符串中加载带有MSXML的DOM对象时,请使用loadXML方法,而不要使用load,后者希望后者被保存文件放在磁盘或URL路径上.

Whenever loading a DOM object with MSXML from string as you are with recordset call, use the loadXML method rather than load which the latter expects a saved file on disk or url path.

所以只需更改:

xslDoc.Load daoRST.Fields("XML").Value

收件人:

xslDoc.LoadXML daoRST.Fields("XML").Value


顺便说一句,您不需要在每次循环迭代时重新加载XSLT,而只需在外部一次加载,但是XML对象应该在循环内部重新初始化,而不是在外部重新初始化.考虑进行以下调整:


By the way, you should not need to re-load XSLT with each iteration of loop but only once outside but the XML objects should be re-initialized inside loop not once outside. Consider following adjustment:

...
' LOAD XSL SCRIPT
xslDoc.LoadXML daoRST.Fields("XML").Value
Set fd = Application.FileDialog(msoFileDialogFilePicker)

With fd
    .InitialFileName = "C:\Users\1122335\Desktop\*.xml"
    If .Show = -1 Then
        For Each vrtSelectedItem In .SelectedItems
            ' INITIALIZE XML OBJECTS
            Set xmlDoc = New MSXML2.DOMDocument60
            Set newDoc = New MSXML2.DOMDocument60

            ' LOAD XML SOURCE
            xmlDoc.Load vrtSelectedItem
            ' TRANSFORM SOURCE
            xmlDoc.transformNodeToObject xslDoc, newDoc
            newDoc.Save "C:\Users\1122335\Desktop\temp.xml"

            ' APPEND TO TABLES
            On Error Resume Next
            Application.ImportXML "C:\Users\1122335\Desktop\temp.xml", acAppendData
        Next vrtSelectedItem                        
    End If
End With

' FREE RESOURCES
Set xmlDoc = Nothing
Set newDoc = Nothing
Set xslDoc = Nothing