【问题标题】:Transforming XML using an XSLT使用 XSLT 转换 XML
【发布时间】:2017-02-17 23:21:02
【问题描述】:

我在 Access 中有一个表,其中有一个包含我的 XSLT 代码的字段。我想导入一个 XML 文档并使用表中的 XSLT 对其进行转换。但是,我收到以下错误:

'-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

【问题讨论】:

  • 您能否在将xslDoc 用于转换之前输出其内容并将其添加到您的问题中?此外,请参阅 stackoverflow.com/questions/23629754/… 和类似的谷歌点击以获取您的错误消息。

标签: vba ms-access xslt


【解决方案1】:

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

所以简单地改变:

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

【讨论】:

  • 谢谢@Parfait!那解决了!我一直在寻找这个答案2天!也谢谢你的调整!
猜你喜欢
  • 1970-01-01
  • 2011-08-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多