从Access导出到XML时,从子元素中省略外键值

时间:2015-12-30 09:44:58

标签: xml vba ms-access access-vba ms-access-2010

我有两个表CR和RELEASE,从CR(ID)到RELEASE(CRID)的关系是一对多。

CR(设计):

ID: key, unique
Description: text

RELEASE(设计):

ID: key, unique
CRID: number, not unique
Name: text

使用以下VBA代码,我设法将表导出为XML。

Set objOrderInfo = Application.CreateAdditionalData
objOrderInfo.Add ("RELEASE")
Application.ExportXML ObjectType:=acExportTable, DataSource:="CR", _
                      DataTarget:=pFileNAme, _
                      AdditionalData:=objOrderInfo

导出的XML类似于:

<?xml version="1.0" encoding="UTF-8"?>
<dataroot xmlns:od="urn:schemas-microsoft-com:officedata" generated="2015-12-09T09:34:28">
<CR>
    **<ID>1</ID>**
    <Description>Test</Description>
    <RELEASE>
        <ID>1</ID>
        **<CRID>1</CRID>**
        <Name>R2016</Name>
    </RELEASE>
    <RELEASE>
        <ID>2</ID>
        **<CRID>1</CRID>**
        <Name>R2017</Name>
    </RELEASE>
</CR>

请注意,CRID在XML中多次显示,这实际上是多余的。如何从XML中的RELEASE元素中删除CRID元素?谢谢,

2 个答案:

答案 0 :(得分:3)

如果您需要在使用<style> #color { background-color: red; } #grayscale1 { filter: grayscale(100%); } #grayscale2 { opacity: 0.4; filter: alpha(opacity=40); } </style> <div id="color"> <div id="grayscale1"> <img src="C:/img.png" /> </div> </div> 后调整XML输出,则可以初始导出到临时XML文件,然后使用.xslt文件,如下所示:

Application.ExportXML

和像这样的VBA例程

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <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="RELEASE/CRID">
        <!-- omit by doing nothing -->
    </xsl:template>

</xsl:stylesheet>

Option Compare Database Option Explicit Public Sub ApplyXmlTransform(sourceFile, stylesheetFile, resultFile) ' ref: http://archive.oreilly.com/pub/post/transforming_xml_in_microsoft.html ' ' project reference required: Microsoft XML, v4.0 Dim source As New MSXML2.DOMDocument30 Dim stylesheet As New MSXML2.DOMDocument30 Dim result As New MSXML2.DOMDocument30 ' Load data. source.async = False source.Load sourceFile ' source .xml file ' Load style sheet. stylesheet.async = False stylesheet.Load stylesheetFile ' .xslt file If (source.parseError.errorCode <> 0) Then MsgBox ("Error loading source document: " & source.parseError.reason) Else If (stylesheet.parseError.errorCode <> 0) Then MsgBox ("Error loading stylesheet document: " & stylesheet.parseError.reason) Else ' Do the transform. source.transformNodeToObject stylesheet, result result.Save resultFile ' resulting .xml file End If End If End Sub 元素中删除<CRID>

答案 1 :(得分:1)

我无法看到你的表现。毕竟,CRID是表RELEASE的一个字段:

<RELEASE>
    <ID>1</ID>
    <CRID>1</CRID>
    <Name>R2016</Name>
</RELEASE>