是否可以在XSL中提取文件路径和文件名以在转换过程中使用

时间:2019-01-12 09:19:24

标签: xml vba ms-access xslt-1.0

我有一个XSLT文件,该文件可以转换MS Access的多节点XML文件,并且需要捕获XML文件的文件名。我想将其用作转换过程中的附加列,以添加到正在填充的表中。理想情况下,我想将标签插入“ INTERFACE_BATCH”节点或PART_DETAIL节点。

<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="INTERFACE_BATCH">
    <xsl:apply-templates select="@*|node()"/>
</xsl:template>

<xsl:template match="PART_DETAIL">
    <PART_DETAIL>
        <ERP_NUMBER><xsl:value-of select="../ERP_NUMBER"/></ERP_NUMBER>
        <PLANT><xsl:value-of select="../PLANT"/></PLANT>
        <PROCESS><xsl:value-of select="../PROCESS"/></PROCESS>
        <STORAGE_LOCATION><xsl:value-of select="../STORAGE_LOCATION"/></STORAGE_LOCATION>
        <xsl:apply-templates select="@*|node()"/>
    </PART_DETAIL>
</xsl:template>

EDIT POST:这是我用来上传和转换XML数据的VBA数据

 Private Sub btnImportData_Click()
'transform and load xml files into tables
Dim strFile As String, strPath As String
Dim xmlDoc As New MSXML2.DOMDocument60, xslDoc As New MSXML2.DOMDocument60
Dim newDoc As New MSXML2.DOMDocument60

    strPath = "\\linbs002\ABI Master Data\ES_XML\"
    strFile = Dir(strPath & "*.xml")

' LOAD XSL ONLY ONCE
    xslDoc.Load strPath & "material_new.xsl"
    On Error Resume Next
    DoCmd.SetWarnings False
    While strFile <> ""
' REINITIALIZE DOM OBJECTS
        Set xmlDoc = New MSXML2.DOMDocument60
        Set newDoc = New MSXML2.DOMDocument60

' LOAD XML SOURCE
        xmlDoc.Load strPath & strFile

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

' APPEND TO TABLES
        Application.ImportXML strPath & "temp.xml", acAppendData

        strFile = Dir()
    Wend
    DoCmd.SetWarnings True

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

'delete unneeded tables
    DoCmd.DeleteObject acTable, "DESCRIPTOR"
    DoCmd.DeleteObject acTable, "DESCRIPTOR_PROPERTY"
    DoCmd.DeleteObject acTable, "ImportErrors"

'append data to table ES_Materials
    DoCmd.OpenQuery "EngineeringSpares", acViewNormal

'clear data from temp tables
    DoCmd.RunSQL "DELETE MATERIAL_ITEM.* FROM MATERIAL_ITEM;"
    DoCmd.RunSQL "DELETE PART_DETAIL.* FROM PART_DETAIL;"

'delete xml files from folder
    Kill strPath & "*.XML"

'advise job complete
    MsgBox "Load & Transform complete", vbInformation, "XML data upload"

End Sub

编辑帖子: 我尝试添加@ June7建议的更新字符串,但没有成功

' APPEND TO TABLES
    Application.ImportXML strPath & "temp.xml", acAppendData

    Debug.Print strPath & strFile
    rst.Edit
    CurrentDb.Execute "UPDATE ES_Materials SET FilePath='" & strPath & strFile & "' WHERE FilePath Is Null"
    rst.MoveNext
    rst.Update

1 个答案:

答案 0 :(得分:0)

建议对新导入的记录运行UPDATE操作。我更喜欢CurrentDb.Execute而不是DoCmd.RunSQL。由于一个原因,不需要使用DoCmd.SetWarnings。

CurrentDb.Execute "UPDATE ES_Materials SET FilePath='" & strPath & strFile & "' WHERE FilePath Is Null"

创建记录时,UPDATE假定FilePath字段为null。如果可能的字段具有空字符串,请尝试其他条件:
"' WHERE FilePath & '' = ''"