Microsoft KB Archive/285329

= You cannot import attribute-centric XML in Access =

Article ID: 285329

Article Last Modified on 3/29/2007

-

APPLIES TO


 * Microsoft Office Access 2007
 * Microsoft Office Access 2003
 * Microsoft Access 2002 Standard Edition

-



This article was previously published under Q285329



Advanced: Requires expert coding, interoperability, and multiuser skills.

This article applies to a Microsoft Access database (.mdb) file or to a Microsoft Access database (.accdb) file.



SYMPTOMS
When you import an XML document, at least one blank table is imported or not all of the data is imported. Depending on the structure of the XML document, more than one table may actually be imported.



CAUSE
If a blank table is imported, it typically indicates that the data of the source XML document is attribute-centric. Microsoft Access supports only element-centric XML. XML that is persisted from ADO recordsets is created in attribute-centric XML.



RESOLUTION
This article shows you how to import XML data that is created by persisting an ADO Recordset to its XML format. The transform that is supplied in this article applies to the ADO XML persisted format.

Microsoft provides programming examples for illustration only, without warranty either expressed or implied. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to debug procedures. Microsoft support engineers can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific requirements. In order to import attribute-centric XML into Access, you must first create and apply an XML Transformation (XSLT) to the source document. This process creates a new XML document that is element-centric and can be imported into Access.

Create an XML Document from ADO
 Create a new blank database and name it ImportADOXML.mdb. On the Insert menu, click Module.

Note In Access 2007, click Macro in the Other group on the Create tab, and then click Module.  In the Visual Basic Editor, type or paste the following code into the new module: Sub CreateADOXML 'Persists an ADO recordset to XML Dim cn As ADODB.Connection Dim rs As ADODB.Recordset 'Open an ADO Connection object 'If the path to Northwind differs on your machine, you will need to   'adjust the Data Source property accordingly. Set cn = New ADODB.Connection With cn       .Provider = &quot;Microsoft.Jet.OLEDB.4.0&quot; .ConnectionString = &quot;Data Source=C:\Program Files\Microsoft &quot; & _ &quot;Office\Office10\Samples\Northwind.mdb&quot; .Open End With 'Open an ADO Recordset Set rs = New ADODB.Recordset With rs       Set .ActiveConnection = cn        .Source = &quot;SELECT * FROM Customers WHERE Country='UK'&quot; .CursorLocation = adUseServer .CursorType = adOpenForwardOnly .LockType = adLockReadOnly .Open 'persist the recordset to XML .Save &quot;C:\ado_customersUK.xml&quot;, adPersistXML .Close End With 'Cleanup cn.Close Set rs = Nothing Set cn = Nothing End Sub  Save the module as basCreateADOXML. In the Visual Basic Editor, click Immediate Window on the View menu to open the Immediate window.  Type the following code in the Immediate window, and then press ENTER: CreateADOXML </li></ol>

Create the XSL Transform
Because the namespaces that are defined by ADO are not recognized by Access, the following transform defines these namespaces, but excludes them from the resulting output. <ol>  Start Notepad, and then type the following XSLT code: <xsl:stylesheet version=&quot;1.0&quot; xmlns:xsl=&quot;http://www.w3.org/1999/XSL/Transform&quot; xmlns:rs=&quot;urn:schemas-microsoft-com:rowset&quot; exclude-result-prefixes=&quot;rs&quot;> <xsl:output method=&quot;xml&quot; version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; indent=&quot;yes&quot;/> <xsl:template match=&quot;/&quot;> <rootElement xmlns:z=&quot;#RowsetSchema&quot; xsl:exclude-result-prefixes=&quot;z&quot;>

<xsl:for-each select=&quot;/xml/rs:data/z:row&quot;> <TableName> <xsl:for-each select=&quot;@*&quot;> <xsl:element name=&quot;{name}&quot;> <xsl:value-of select=&quot;.&quot;/> </xsl:element>

</xsl:for-each> </TableName>

</xsl:for-each> </rootElement> </xsl:template> </xsl:stylesheet>

</li> Save the document as ADOXMLToAccess.xsl in the same folder to which you save the ImportADOXML.mdb database.</li></ol>

Apply the Transform and Import
To apply the XSL transform, you must use an XSLT processor such as the Microsoft MSXML3 processor, which is installed with Microsoft Office XP. The following steps use the XML Document Object Model to apply the transform that you created earlier to an ADO XML document and to import it into Access.

NOTE: The sample code in this article uses the XML Document Object Model. For this code to run properly, you must reference the Microsoft XML 3.0 library. To do so, click References on the Tools menu in the Visual Basic Editor, and ensure that the Microsoft XML, v3.0 check box is selected. <ol> Start Microsoft Access and open the ImportADOXML.mdb database that you created earlier.</li> In the Database window, click Modules under Objects, and then click New.

Note In Access 2007, click Macro in the Other group on the Create tab, and then click Module.</li>  In the Visual Basic Editor, type or paste the following code in the module: Sub ImportXMLFromADO 'Uses the XML DOM to transform XML from ADO 'to element-centric XML and imports into Access Dim domIn As DOMDocument30 Dim domOut As DOMDocument30 Dim domStylesheet As DOMDocument30 Set domIn = New DOMDocument30 domIn.async = False 'Open the ADO xml document If domIn.Load(&quot;C:\ado_customersUK.xml&quot;) Then 'Load the stylesheet 'In this example you will need to change <PathToStylesheet> to       'the actual path where you stored the ADOXMLToAccess.xsl file. Set domStylesheet = New DOMDocument30 domStylesheet.Load &quot;<PathToStylesheet>\ADOXMLToAccess.xsl&quot; 'Apply the transform If Not domStylesheet Is Nothing Then Set domOut = New DOMDocument30 domIn.transformNodeToObject domStylesheet, domOut 'Save the output domOut.Save &quot;c:\customersUK.xml&quot; 'Import the saved document into Access Application.ImportXML &quot;c:\customersUK.xml&quot; End If   End If    'Cleanup Set domIn = Nothing Set domOut = Nothing Set domStylesheet = Nothing MsgBox &quot;done!&quot;,, &quot;ImportXMLFromADO&quot; End Sub </li> In the Visual Basic Editor, click Immediate Window on the View menu to open the Immediate Window.</li>  Type the following code in the Immediate Window, and then press ENTER: ImportXMLFromADO Note that a table containing customers from the UK with the name that you specified for <TableName> is imported into Access. You may optionally delete the element-centric XML document that is created during the transformation. </li></ol>

<div class="status_section">

STATUS
This behavior is by design.

<div class="references_section">