2017-10-15 55 views
1

我正在使用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> 
+0

導入Access的結果,您可以只使用DOM獲得各元素的含量,並手動解析。請參閱[如何使用VBA解析XML](https://stackoverflow.com/questions/11305/how-to-parse-xml-using-vba) –

+0

當然只有一個確實,列必須是唯一的。請發佈需要修改每個* UnsafeProcedures *的XSLT。 – Parfait

+0

它看起來像你的XML樣本是你當前轉換的結果(創建'my:tblUnsafeProcedures')。您現在是否在尋找第二個XSLT文件來進一步轉換您的數據?如果不是,那麼* original * XML(包括根節點)的示例可能會有所幫助。 –

回答

1

在你原來的問題

<?xml version="1.0" encoding="UTF-8"?> 
<dataroot xmlns:my="http://schemas.microsoft.com/office/infopath/2003/myXSD/2016-02-16T15:55:31"> 
    <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> 
    <my:myFields> 
     <my:DateOfIncident>2001-02-03T04:05:06</my:DateOfIncident> 
     <my:IncidentID>20010123T1234</my:IncidentID> 
     <my:IncidentLocation>Space</my:IncidentLocation> 
     <my:InvolvedPerson>HAL 9000</my:InvolvedPerson> 
     <my:tblUnsafeProcedures> 
      <my:IncidentID>20010123T1234</my:IncidentID> 
      <my:UnsafeProcedures>Killed crew member</my:UnsafeProcedures> 
      <my:UnsafeProcedures>Refused to open pod bay doors</my:UnsafeProcedures> 
     </my:tblUnsafeProcedures> 
    </my:myFields> 
</dataroot> 

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

    <xsl:template match="my:UnsafeProcedures"> 
    <my:tblUnsafeProcedures> 
     <my:IncidentID> 
     <xsl:value-of select="../my:IncidentID"/> 
     </my:IncidentID> 
     <my:Procedure> 
     <xsl:value-of select="node()"/> 
     </my:Procedure> 
    </my:tblUnsafeProcedures> 
    </xsl:template> 

</xsl:stylesheet> 

基於XML樣本的充實出的版本產生以下XML輸出

<?xml version="1.0" encoding="UTF-16"?> 
<dataroot xmlns:my="http://schemas.microsoft.com/office/infopath/2003/myXSD/2016-02-16T15:55:31"> 
    <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:IncidentID>20170125T1452</my:IncidentID> 
     <my:tblUnsafeProcedures> 
      <my:IncidentID>20170125T1452</my:IncidentID> 
      <my:Procedure>Other (explain)</my:Procedure> 
     </my:tblUnsafeProcedures> 
     <my:tblUnsafeProcedures> 
      <my:IncidentID>20170125T1452</my:IncidentID> 
      <my:Procedure>Unsafe Lifting</my:Procedure> 
     </my:tblUnsafeProcedures> 
     <my:tblUnsafeProcedures> 
      <my:IncidentID>20170125T1452</my:IncidentID> 
      <my:Procedure>Using equipment unsafely</my:Procedure> 
     </my:tblUnsafeProcedures> 
    </my:myFields> 
    <my:myFields> 
     <my:DateOfIncident>2001-02-03T04:05:06</my:DateOfIncident> 
     <my:IncidentID>20010123T1234</my:IncidentID> 
     <my:IncidentLocation>Space</my:IncidentLocation> 
     <my:InvolvedPerson>HAL 9000</my:InvolvedPerson> 
     <my:IncidentID>20010123T1234</my:IncidentID> 
     <my:tblUnsafeProcedures> 
      <my:IncidentID>20010123T1234</my:IncidentID> 
      <my:Procedure>Killed crew member</my:Procedure> 
     </my:tblUnsafeProcedures> 
     <my:tblUnsafeProcedures> 
      <my:IncidentID>20010123T1234</my:IncidentID> 
      <my:Procedure>Refused to open pod bay doors</my:Procedure> 
     </my:tblUnsafeProcedures> 
    </my:myFields> 
</dataroot> 

,當在

[myFields] 

DateOfIncident  IncidentID  IncidentLocation InvolvedPerson 
------------------- ------------- ---------------- -------------- 
2017-01-10T16:00:00 20170125T1452 Mayberry   Joe Blow  
2001-02-03T04:05:06 20010123T1234 Space    HAL 9000  

[tblUnsafeProcedures] 

IncidentID  Procedure      
------------- ----------------------------- 
20170125T1452 Other (explain)    
20170125T1452 Unsafe Lifting    
20170125T1452 Using equipment unsafely  
20010123T1234 Killed crew member   
20010123T1234 Refused to open pod bay doors 
+0

我現在看到了。我需要轉換我的原始XML,然後在顯示時再次轉換它。有用!非常感謝! –

+0

您不需要再次轉換它。只需要一個XSLT。替換您的版本。 – Parfait