2017-02-17 282 views
0

我在Access中有一個表中有一個字段,裏面有我的XSLT代碼。我想導入一個XML文檔並使用表中的XSLT進行轉換。不過,我得到以下錯誤:使用XSLT轉換XML

'-2147467259 (800004005)' the stylesheet does not contain a document element. The stylesheet may be empty, or it may not be a well-formed xml document

與XML驗證支票是成功的。

Private Sub btnImport_Click() 
Dim StrFileName As String 
Dim fd As FileDialog 
Dim vrtSelectedItem As Variant 
Dim strFile As String, strPath As String 
Dim xmlDoc As New MSXML2.DOMDocument60, xslDoc As New MSXML2.DOMDocument60 
Dim newDoc As New MSXML2.DOMDocument60 

Dim daoRST As DAO.Recordset: Set daoRST = CurrentDb.OpenRecordset("Attachments"): Debug.Print daoRST.Fields("XML").Value: 

Set xmlDoc = New MSXML2.DOMDocument60 
Set newDoc = New MSXML2.DOMDocument60 
Set fd = Application.FileDialog(msoFileDialogFilePicker) 

    With fd 
     .InitialFileName = "C:\Users\1122335\Desktop\*.xml" 
     If .Show = -1 Then 
      For Each vrtSelectedItem In .SelectedItems 
      xslDoc.Load daoRST.Fields("XML").Value 
      ' LOAD XML SOURCE 
      xmlDoc.Load vrtSelectedItem 
      ' TRANSFORM SOURCE 
      xmlDoc.transformNodeToObject xslDoc, newDoc '<<ERROR HERE 
      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 
     Else 
     End If 
    End With 
End Sub 

在此行中出現的錯誤:

xmlDoc.transformNodeToObject xslDoc, newDoc 
+0

您是否可以在將其用於轉換之前輸出'xslDoc'的內容並將其添加到您的問題中?此外,請參閱https://stackoverflow.com/questions/23629754/script16389-the-stylesheet-does-not-contain-a-document-element-the-stylesheet以及類似的谷歌匹配您的錯誤消息。 – Leviathan

回答

2

每當有MSXML從字符串加載DOM對象,你是有記錄的呼叫,使用loadXML方法,而不是load後者預計保存磁盤或網址路徑中的文件。

所以,簡單地改變:

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

要:

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

順便說一句,你不應該需要重新加載XSLT與循環,但只有一次,每次迭代外部,但是XML對象應該在循環內部重新初始化,而不是在外部。考慮進行以下調整:

... 
' 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 
+1

謝謝@Parfait!這解決了它!我一直在尋找2天的答案!也感謝您的調整! – 1122335