我正在使用XSLT将一些XML文档(在InfoPath中创建)导入MS 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表单。 UnsafeProcedures字段是一个多选列表框,有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 :(得分:1)
基于原始问题中的XML样本的充实版本
<?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 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>
导入到Access中时导致
[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