MS Access to XML 'Saved Export' 在重新导出时产生不同的 XML 结构

MS Access to XML 'Saved Export' produces different XML structure when re-exported

我有一个用于管理课程数据的 Microsoft Access 数据库(通过 Office 2010),我需要定期将此数据导出到特定的 XML 结构。

不幸的是,我发现当 'saved export' 后来重新 运行 时,通过初始 XML 导出生成的结构不会被复制。我进行了大量测试并尝试在线研究此问题,但没有找到解决方案。希望有人能帮忙!

相关的数据库部分如下所示:(这已从原始数据库中简化,但仍然出现同样的问题)。

一些上下文信息:'Course' 是总体培训计划,可能会 运行 多次。 'Occurrence' 是学生实际注册的课程的特定 instance/running。 'Unit' 是该事件的特定教学部分(例如实践、研讨会)。 'Units' 可以构成多个 'Occurrences' 的一部分,因此需要多对多桥接 table 'OccurrencesUnits'。这个结构是经过仔细规划的,我们实际上无法改变它(希望不需要)。

当我导出到 XML(外部数据 > XML 文件)时,我 select 从所有这些 table 中导出数据:

初始导出完成后,生成的 XML 如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<dataroot xmlns:od="urn:schemas-microsoft-com:officedata" generated="2015-11-25T16:09:31">
        <Courses>
                <CourseID>1</CourseID>
                <CourseTitle>Meat Science</CourseTitle>
                <Occurrences>
                        <OccurrenceID>1</OccurrenceID>
                        <OccurrenceTitle>Meat Science - 10 Credit Version</OccurrenceTitle>
                        <CourseID>1</CourseID>
                        <OccurrencesUnits>
                                <OccurrencesUnitsID>1</OccurrencesUnitsID>
                                <OccurrenceID>1</OccurrenceID>
                                <UnitID>1</UnitID>
                        </OccurrencesUnits>
                </Occurrences>
                <Occurrences>
                        <OccurrenceID>2</OccurrenceID>
                        <OccurrenceTitle>Meat Science - 20 Credit Version</OccurrenceTitle>
                        <CourseID>1</CourseID>
                        <OccurrencesUnits>
                                <OccurrencesUnitsID>2</OccurrencesUnitsID>
                                <OccurrenceID>2</OccurrenceID>
                                <UnitID>1</UnitID>
                        </OccurrencesUnits>
                                <OccurrencesUnits>
                                <OccurrencesUnitsID>3</OccurrencesUnitsID>
                                <OccurrenceID>2</OccurrenceID>
                                <UnitID>2</UnitID>
                        </OccurrencesUnits>
                </Occurrences>
        </Courses>
        <Units>
                <UnitID>1</UnitID>
                <UnitTitle>Meat Safety</UnitTitle>
        </Units>
        <Units>
                <UnitID>2</UnitID>
                <UnitTitle>Meat Genetics</UnitTitle>
        </Units>
</dataroot>

这正是我们想要的正确数据结构,其中 'OccurrencesUnits' 嵌套在 'Occurrences' 中。然后我保存导出步骤以便稍后在 MS Access 中使用。

不幸的是,每当我重新使用此导出步骤(通过外部数据 > 保存的导出)时,问题就会出现。这会生成结构不同的 XML 文件:

<?xml version="1.0" encoding="UTF-8"?>
<dataroot xmlns:od="urn:schemas-microsoft-com:officedata" generated="2015-11-25T16:10:34">
        <Courses>
                <CourseID>1</CourseID>
                <CourseTitle>Meat Science</CourseTitle>
                <Occurrences>
                        <OccurrenceID>1</OccurrenceID>
                        <OccurrenceTitle>Meat Science - 10 Credit Version</OccurrenceTitle>
                        <CourseID>1</CourseID>
                </Occurrences>
                <Occurrences>
                        <OccurrenceID>2</OccurrenceID>
                        <OccurrenceTitle>Meat Science - 20 Credit Version</OccurrenceTitle>
                        <CourseID>1</CourseID>
                </Occurrences>
        </Courses>
        <OccurrencesUnits>
                <OccurrencesUnitsID>1</OccurrencesUnitsID>
                <OccurrenceID>1</OccurrenceID>
                <UnitID>1</UnitID>
        </OccurrencesUnits>
                <OccurrencesUnits>
                <OccurrencesUnitsID>2</OccurrencesUnitsID>
                <OccurrenceID>2</OccurrenceID>
                <UnitID>1</UnitID>
        </OccurrencesUnits>
        <OccurrencesUnits>
                <OccurrencesUnitsID>3</OccurrencesUnitsID>
                <OccurrenceID>2</OccurrenceID>
                <UnitID>2</UnitID>
        </OccurrencesUnits>
        <Units>
                <UnitID>1</UnitID>
                <UnitTitle>Meat Safety</UnitTitle>
        </Units>
        <Units>
                <UnitID>2</UnitID>
                <UnitTitle>Meat Genetics</UnitTitle>
        </Units>
</dataroot>

在此版本中,您可以看到 'OccurrencesUnits' 不再位于 'Occurrences' 内,现在完全位于 'Occurrences' 和 'Courses' 标签之外。这给我们带来了很大的困难,因为我们需要在原始结构中使用它。

我不明白为什么 'Saved Export' 实际上没有复制保存它的原始导出。这可能是 MS Access 中的错误,还是我做错了什么?我曾多次尝试重新创建数据库,也曾尝试导出 with/without 一个 XML 架构,但找不到解决方案。

希望有人能帮忙 - 非常感谢!非常感谢。

我无法在 MS Access 中完全重现您的问题。我通过手册 XML 导出向导和保存的导出都产生了相同的结果,即前两个相关 tables(课程和事件)嵌套在最后一个 table 中的最后一种格式(OccurrenceUnits) 堆叠在最后。我可以说的是,您保存的导出取决于 table 之间已建立的关系,而不仅仅是 table 的导出或 table 的内部联接 SQL 查询。

但是,请考虑一种专门自动化您的确切结构需求的编程解决方案。你需要的是 XSLT (the declarative programming language that re-styles, re-formats, re-structures XML documents). VBA can transform XML with XSLT scripts using the MSXML 对象。您可以继续 运行 已保存的导出(使用 table 关系),然后加载外部 XML 和 XSLT 文件进行转换。

XSLT (另存为外部 .xsl 或 .xslt 以在下面使用)

<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
               xmlns:od="urn:schemas-microsoft-com:officedata"
               xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"               
               exclude-result-prefixes="od xsi">

<xsl:output version="1.0" encoding="UTF-8" indent="yes" />
<xsl:strip-space elements="*"/>

  <!-- Identity Transform -->
  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="Courses">
    <Courses>
        <xsl:copy-of select="CourseID"/>
        <xsl:copy-of select="CourseTitle"/>
        <xsl:for-each select="Occurences">
            <xsl:copy-of select="*"/>
            <xsl:variable name="occid" select="OccurenceID"/>
            <xsl:copy-of select="../../OccurrencesUnits[OccurenceID=$occid]"/>
        </xsl:for-each>        
    </Courses>
  </xsl:template>

  <xsl:template match="OccurrencesUnits"/>

</xsl:transform>

VBA子程序(在命令按钮或触发事件后面使用)

下面是 XML 导出的两个选项:1) 使用您保存的导出对象或 2) 自动 Application.ExportXML 您将其他相关的 table 添加到课程输出:

Private Sub XMLOutput_Click()

    Dim rawDoc As Object, xslDoc As Object, newDoc As Object
    Dim xmlstr As String, xslstr As String
    Dim otherTables As AdditionalData

    Set otherTables = Application.CreateAdditionalData
    otherTables.Add "Occurrences"
    otherTables.Add "OccurrencesUnits"        
    otherTables.Add "Units"        

    ' RUN SAVED XML EXPORT ' 
    'DoCmd.RunSavedImportExport "SavedXMLExportName"

    ' RUN AUTOMATED EXPORT XML WITH NESTED ADDITIONAL TABLES ' 
    Application.ExportXML acExportTable, "Courses", "C:\Path\To\Original\XMLfile.xml", _
                 , , , , , , AdditionalData:=otherTables

    ' LOAD XML AND XSL FILES '
    xmlstr = "C:\Path\To\Original\XMLfile.xml"
    xslstr = "C:\Path\To\XSLT\Script.xsl"

    Set rawDoc = CreateObject("MSXML2.DOMDocument")
    Set xslDoc = CreateObject("MSXML2.DOMDocument")
    Set newDoc = CreateObject("MSXML2.DOMDocument")

    rawDoc.async = False
    rawDoc.Load xmlstr

    xslDoc.async = False
    xslDoc.Load xslstr

    ' TRANSFORM TO NEW XML '
    rawDoc.transformNodeToObject xslDoc, newDoc

    ' SAVE NEW XML FILE '
    newDoc.Save "C:\Path\To\Output\XMLfile.xml"

    MsgBox "Successfully transformed xml!", vbInformation

End Sub