我有一个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>
编辑帖子:这是我用来上传和转换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 = "\linbs002ABI Master DataES_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
建议对新导入的记录运行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 & '' = ''"