我正在使用XSLT將一些XML文檔(在InfoPath中創建)導入MS Access。除了一個問題,它工作正常。許多嵌套元素(子表)包含多個值,但導入只引入一條記錄。將具有多個嵌套行的XML導入Access
在此示例中,有3個UnsafeProcedures,但只有第一個被導入到tblUnsafeProcedures表中。
XML
<my:myFields
<my:DateOfIncident>2017-01-10T16:00:00</my:DateOfIncident>
<my:IncidentID>20170125T1452</my:IncidentID>
<my:IncidentLocation>Mayberry</my:IncidentLocation>
<my:InvolvedPerson>Joe Blow</my:InvolvedPerson>
<my:tblUnsafeProcedures>
<my:IncidentID>20170125T1452</my:IncidentID>
<my:UnsafeProcedures>Other (explain)</my:UnsafeProcedures>
<my:UnsafeProcedures>Unsafe Lifting</my:UnsafeProcedures>
<my:UnsafeProcedures>Using equipment unsafely</my:UnsafeProcedures>
</my:tblUnsafeProcedures>
</my:myFields>
這裏是VBA運行進口
Public Sub TransformAndImportMultipleXMLs()
On Error GoTo Err_Trap
Dim strFile As String, strPath As String
Dim xmlDoc As New MSXML2.DOMDocument60
Dim xslDoc As New MSXML2.DOMDocument60
Dim newDoc As New MSXML2.DOMDocument60strPath = "y:\"
strFile = Dir(strPath & "*.xml")
xslDoc.async = False
xslDoc.validateOnParse = False
xslDoc.resolveExternals = False
xslDoc.Load "C:\JSTAdb\IncidentID.xslt"
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 "C:\JSTAdb\temp.xml"
' APPEND TO TABLES
Application.ImportXML "C:\JSTAdb\temp.xml", acAppendData
Call WaitFor(1) 'Wait for 1 second between imports
strFile = Dir()
Wend
' RELEASE DOM OBJECTS
Set xmlDoc = Nothing: Set xslDoc = Nothing: Set newDoc = Nothing
Err_Trap:
Debug.Print Error
Resume Next
End Sub
必須有一種方法來導入所有這些子元素。我錯過了什麼?
XSLT
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:my="http://schemas.microsoft.com/office/infopath/2003/myXSD/2016-02-16T15:55:31"
xmlns:xd="http://schemas.microsoft.com/office/infopath/2003"
xml:lang="en-US">
<xsl:output indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="my:grpUnsafeWorkProcedures">
<my:tblUnsafeProcedures>
<my:IncidentID><xsl:value-of select="../my:IncidentID"/></my:IncidentID>
<xsl:apply-templates select="@*|node()"/>
</my:tblUnsafeProcedures>
</xsl:template>
</xsl:stylesheet>
下面是改造前的原始XML文件。這是一個InfoPath表單。不安全程序字段是一個多選列表框,其中有7個項目可供選擇。在這種情況下,選擇了3個項目。
原始XML
<?xml version="1.0" encoding="utf-8"?>
<?mso-infoPathSolution name="urn:schemas-microsoft-com:office:infopath:INCIDENT-INVESTIGATION-REPORTS:-myXSD-2016-02-16T15-55-31" solutionVersion="1.0.0.1597" productVersion="15.0.0.0" PIVersion="1.0.0.0" href="http://sharepoint.acme.com/sites/safety/RecAndNonRec/Forms/template.xsn"?>
<?mso-application progid="InfoPath.Document" versionProgid="InfoPath.Document.4"?>
<?mso-infoPath-file-attachment-present?>
<my:myFields my:Why4="Joe messed up" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:pc="http://schemas.microsoft.com/office/infopath/2007/PartnerControls" xmlns:ma="http://schemas.microsoft.com/office/2009/metadata/properties/metaAttributes" xmlns:d="http://schemas.microsoft.com/office/infopath/2009/WSSList/dataFields" xmlns:q="http://schemas.microsoft.com/office/infopath/2009/WSSList/queryFields" xmlns:dfs="http://schemas.microsoft.com/office/infopath/2003/dataFormSolution" xmlns:dms="http://schemas.microsoft.com/office/2009/documentManagement/types" xmlns:my="http://schemas.microsoft.com/office/infopath/2003/myXSD/2016-02-16T15:55:31" xmlns:xd="http://schemas.microsoft.com/office/infopath/2003" xml:lang="en-US">
<my:DateOfIncident>2017-01-10T16:00:00</my:DateOfIncident>
<my:IncidentLocation>Maberry</my:IncidentLocation>
<my:InvolvedPerson>Joe Blow</my:InvolvedPerson>
<my:grpUnsafeWorkProcedures>
<my:UnsafeProcedures></my:UnsafeProcedures><my:UnsafeProcedures>Other (explain)</my:UnsafeProcedures><my:UnsafeProcedures>Unsafe Lifting</my:UnsafeProcedures><my:UnsafeProcedures>Using equipment unsafely</my:UnsafeProcedures>
</my:grpUnsafeWorkProcedures>
<my:IncidentID>20170125T1452</my:IncidentID>
</my:myFields>
導入Access的結果,您可以只使用DOM獲得各元素的含量,並手動解析。請參閱[如何使用VBA解析XML](https://stackoverflow.com/questions/11305/how-to-parse-xml-using-vba) –
當然只有一個確實,列必須是唯一的。請發佈需要修改每個* UnsafeProcedures *的XSLT。 – Parfait
它看起來像你的XML樣本是你當前轉換的結果(創建'my:tblUnsafeProcedures')。您現在是否在尋找第二個XSLT文件來進一步轉換您的數據?如果不是,那麼* original * XML(包括根節點)的示例可能會有所幫助。 –