将XML数据导入MS Access运行时错误

时间:2018-10-03 14:39:50

标签: xml vba ms-access import stylesheet

使用MS Access向导导入时,一切正常。我指定了创建两个表的XML和XSLT文件。保存导入步骤并尝试运行失败。这分别是示例XML和XSLT文件。 MS Access使用PWSID和VIOID值作为键,将错误作为单独的表引入。

    <?xml version="1.0"?>
    <Violations>
     <Violation>
      <ViolationDetail>
       <PWSID>TX9999999</PWSID>
       <VIOID>123456789</VIOID>
       <DATAORIGINCODE>S</DATAORIGINCODE>
       <CCODE>7500</CCODE>
       <VCODE>75</VCODE>
       <BEGINDATE>2015-04-28</BEGINDATE>
       <Errors>
          <Error>
            <ErrorDescription>
                Error description here
            </ErrorDescription>
            <ErrorCode>ERR0037</ErrorCode>
            </Error>
       </Errors>
      </ViolationDetail>
     </Violation>
     <Violation>
      <ViolationDetail>
       <PWSID>TX9999999</PWSID>
       <VIOID>234567890</VIOID>
       <DATAORIGINCODE>S</DATAORIGINCODE>
       <CCODE>7500</CCODE>
       <VCODE>71</VCODE>
       <BEGINDATE>2015-05-15</BEGINDATE>
       <Errors>
          <Error>
            <ErrorDescription>
                Error description here
            </ErrorDescription>
            <ErrorCode>ERR0048</ErrorCode>
            </Error>
       </Errors>
      </ViolationDetail>
     </Violation>
    </Violations>

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
        <xsl:output indent="yes"/>
        <xsl:strip-space elements="*"/>

        <xsl:template match="/">
            <dataroot>
                <xsl:apply-templates select="@*|node()"/>
            </dataroot>
        </xsl:template>

        <xsl:template match="@*|node()">
            <xsl:copy>
                <xsl:apply-templates select="@*|node()"/>
            </xsl:copy>
        </xsl:template>

        <xsl:template match="ERRORS">
            <xsl:apply-templates select="@*|node()"/>
        </xsl:template>

        <xsl:template match="ERROR">
            <ERROR>
                <PWSID><xsl:value-of select="../../PWSID"/></PWSID>
                <xsl:apply-templates select="@*|node()"/>
                <VIOID><xsl:value-of select="../../VIOID"/></VIOID>
                <xsl:apply-templates select="@*|node()"/>
            </ERROR>
        </xsl:template>

    </xsl:stylesheet>

我尝试使用VBA代码是通过搜索解决方案修改的:

Public Sub importErrorFiles()
    ' REFERENCE MS XML, v6.0
    Dim xmlDoc As New MSXML2.DOMDocument60
    Dim xslDoc As New MSXML2.DOMDocument60
    Dim newDoc As New MSXML2.DOMDocument60

    Dim strFileXML As String, strFileXSL As String
    Dim strPathXML As String, strPathXSL As String

    strPathXSL = CurrentProject.Path & "\xslSheets\"
    strPathXML = CurrentProject.Path & "\workingReports\"
    strFileXML = Dir(strPathXML & "*.xml")

    While strFileXML <> ""
        ' REINITIALIZE DOM OBJECTS
        Set xslDoc = New MSXML2.DOMDocument60
        Set xmlDoc = New MSXML2.DOMDocument60
        Set newDoc = New MSXML2.DOMDocument60

        strFileXSL = strPathXSL & Replace(strFileXML, "xml", "xslt")

        If fileExists(strFileXSL) Then

            ' LOAD XML SOURCE
            xmlDoc.Load strPathXML & strFileXML

            ' LOAD XSL FILE
            xslDoc.Load strPathXSL & strFileXSL

            ' TRANSFORM SOURCE
            xmlDoc.transformNodeToObject xslDoc, newDoc
            newDoc.Save strPathXML & "temp.xml"

            ' APPEND TO TABLES
            Application.ImportXML strPathXML & "temp.xml", acAppendData
            strFile = Dir()
        End If
    Wend

   ' RELEASE DOM OBJECTS
    Set xmlDoc = Nothing: Set xslDoc = Nothing: Set newDoc = Nothing
End Sub

错误发生在transformNodeToObject行:

  

样式表不包含文档元素。样式表似乎为空,或者可能不是格式正确的XML文档。

哦,fileExists是一个确实起作用的布尔函数。 感谢您的帮助。

0 个答案:

没有答案