从 Access 导出到 XML 时省略子元素的外键值
Omit foreign key values from child elements when exporting from Access to XML
我有两个表 CR 和 RELEASE,它们具有从 CR (ID) 到 RELEASE (CRID) 一对多的关系。
CR(设计):
ID: key, unique
Description: text
发布(设计):
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 元素?谢谢,
我看不出你怎么会。毕竟CRID是tableRELEASE:
的字段
<RELEASE>
<ID>1</ID>
<CRID>1</CRID>
<Name>R2016</Name>
</RELEASE>
如果您在使用 Application.ExportXML
后需要调整 XML 输出,您可以将初始导出到临时 XML 文件,然后使用 .xslt 文件,如下所示:
<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>
和这样的 VBA 例程
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
从 <RELEASE>
元素中删除 <CRID>
。
我有两个表 CR 和 RELEASE,它们具有从 CR (ID) 到 RELEASE (CRID) 一对多的关系。
CR(设计):
ID: key, unique
Description: text
发布(设计):
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 元素?谢谢,
我看不出你怎么会。毕竟CRID是tableRELEASE:
的字段<RELEASE>
<ID>1</ID>
<CRID>1</CRID>
<Name>R2016</Name>
</RELEASE>
如果您在使用 Application.ExportXML
后需要调整 XML 输出,您可以将初始导出到临时 XML 文件,然后使用 .xslt 文件,如下所示:
<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>
和这样的 VBA 例程
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
从 <RELEASE>
元素中删除 <CRID>
。