【发布时间】:2016-08-20 01:37:39
【问题描述】:
我有一个 MS Access 数据库,其中包含定期导出为 XML 以提供网站数据的课程数据。
我需要导出的表是一个主要的Courses 表,以及其他几个直接相关和间接相关的表,例如Occurrences、OccurrencesUnits、Units、Locations、Staff、@987654329 @等
在我之前的Stack Overflow question 的帮助下(可能值得阅读一些背景信息),我能够设置所需的确切 XML 结构,简化如下:
<Courses>
<CourseID>1</CourseID>
<CourseTitle>Meat Science</CourseTitle>
(etc etc)
<Occurrences>
<OccurrenceID>1</OccurrenceID>
<OccurrenceTitle>Meat Science Autumn 2016</OccurrenceTitle>
<CourseID>1</CourseID>
<OccurrencesUnits>
<OccurrencesUnitsID>1</OccurrencesUnitsID>
<OccurrenceID>1</OccurrenceID>
<UnitID>1</UnitID>
</OccurrencesUnits>
</Occurrences>
</Courses>
<Courses>
... more courses...
</Courses>
<Units>
...
</Units>
<Locations>
...
</Locations>
<Staff>
...
</Staff>
<SubjectAreas>
...
</SubjectAreas>
etc etc
因此有几层,Occurrences 和 OccurrencesUnits 表嵌套在它们的每个 Courses 条目中,但所有其他相关表需要在下面列出(不嵌套)。
但是,在最近从 Access 2010 升级到 2013 时,我发现我无法生成相同的结构。默认情况下,Access 2013 似乎通过将任何直接相关表嵌套到主表节点中来考虑表关系。当通过 XSL 指定布局结构时(根据我的相关 Stack Overflow 问题,上面链接),这会导致 XML 导出中省略 直接 相关的表,这与 2010 年它们自动出现在Courses.
我发现 accessforums.net 上的另一个人发现了类似的问题:http://www.accessforums.net/showthread.php?t=46933 他们的解决方案是删除 Access 中的表关系,公平地说,这确实会导致预期的 XML 输出,但可能对我的数据库!
关于信息,我在 Access 中的 VBA 导出代码如下所示:
Private Sub ExportCourseCatalogXML_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"
otherTables.Add "Locations"
otherTables.Add "CourseSubcategories"
otherTables.Add "CourseTags"
otherTables.Add "Partners"
otherTables.Add "Staff"
otherTables.Add "SubjectAreas"
Application.ExportXML acExportTable, "Courses", "S:\Science\Biosciences\AATP\Database\xml\course-catalog.xml", _
, , , , , , AdditionalData:=otherTables
' LOAD XML AND XSL FILES '
xmlstr = "C:\path\to\course-catalog.xml"
xslstr = "C:\path\to\structure.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\course-catalog.xml"
MsgBox "Successfully exported XML.", vbInformation
End Sub
而我的structure.xsl 看起来像这样:
<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"/>
(etc etc)
<xsl:for-each select="Occurrences">
<Occurrences>
<xsl:copy-of select="*"/>
<xsl:variable name="occid" select="occurrenceID"/>
<xsl:copy-of select="../../OccurrencesUnits[CourseOccurrence=$occid]"/>
</Occurrences>
</xsl:for-each>
</Courses>
</xsl:template>
<xsl:template match="OccurrencesUnits"/>
</xsl:transform>
所以我的问题 - 是否可以指定(可能是通过 XSL)我需要的确切结构,即除 Occurrences 和 OccurrencesUnits 之外的Courses 列出的所有表外部?非常感谢您对此的任何支持!
2016 年 8 月 17 日添加:
为澄清起见,MS Access 2013 生成的 XML 如下所示。它与 2010 年类似,但问题是任何与 Courses 表直接相关的表都被省略了(CourseSubcategories、CourseTags、Partners、Staff 和 SubjectAreas ),并且包含它们的唯一方法是在 Access 中删除它们与 Courses 的关系。包括Units 和Locations 的表(因为它们与Courses 没有直接关系)。
<Courses>
<CourseID>1</CourseID>
<CourseTitle>Meat Science</CourseTitle>
(etc etc)
<Occurrences>
<OccurrenceID>1</OccurrenceID>
<OccurrenceTitle>Meat Science Autumn 2016</OccurrenceTitle>
<CourseID>1</CourseID>
<OccurrencesUnits>
<OccurrencesUnitsID>1</OccurrencesUnitsID>
<OccurrenceID>1</OccurrenceID>
<UnitID>1</UnitID>
</OccurrencesUnits>
</Occurrences>
</Courses>
<Courses>
... more courses...
</Courses>
<Units>
...
</Units>
<Locations>
...
</Locations>
【问题讨论】:
-
你能显示当前的 XML 输出吗?它是否喜欢您发布的类似问题的链接,其中仅嵌套了其他表中的最后一条记录?
-
现在已经在上面发布了当前的 XML 输出 - 希望对您有所帮助。抱歉,不确定您在第二个问题中的意思 - 我没有注意到表中的单个记录与该表中的其他记录表现不同的任何问题。
-
这些表节点是否显示在原始 XML 中(转换前)?
标签: xml database ms-access xslt export