Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to import XML with nested nodes (parent/child relationships) into Access?

I'm trying to import an XML file into Access but it creates 3 unrelated tables. That is, the child records are imported into the child table, but there is no way of knowing which child records belong to which parent.

How can I import the data to maintain the relationship between the parent and child nodes (records)?

Here is a sample of the XML data:

<NOTARIO>
    <C_NOT>8404180</C_NOT>
    <APE>Abalos Nuevo</APE>
    <NOM>Francisco José</NOM>
    <NOTARIAS>
        <NOTARIA>
            <PRO>23</PRO>
            <MUN>0888</MUN>
            <F_IN>1984-12-01</F_IN>
            <F_FI>1986-09-19</F_FI>
        </NOTARIA>
        <NOTARIA>
            <PRO>14</PRO>
            <MUN>0569</MUN>
            <F_IN>1990-09-17</F_IN>
            <F_FI>1995-03-15</F_FI>
        </NOTARIA>
        <NOTARIA>
            <PRO>21</PRO>
            <MUN>0412</MUN>
            <F_IN>1995-03-30</F_IN>
            <F_FI></F_FI>
        </NOTARIA>
    </NOTARIAS>
</NOTARIO>
like image 315
AMB Avatar asked May 04 '15 11:05

AMB


1 Answers

What you need to do is transform your XML data into a format that works better with Access. Specifically, you need to insert the parent key value (assuming that it is C_NOT in this case) into each child node.

The following XSLT file will do that for you

<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="/">
        <dataroot>
            <xsl:apply-templates select="@*|node()"/>
        </dataroot>
    </xsl:template>

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

    <xsl:template match="NOTARIAS">
        <xsl:apply-templates select="@*|node()"/>
    </xsl:template>

    <xsl:template match="NOTARIA">
        <NOTARIA>
            <C_NOT><xsl:value-of select="../../C_NOT"/></C_NOT>
            <xsl:apply-templates select="@*|node()"/>
        </NOTARIA>
    </xsl:template>

</xsl:stylesheet>

That will transform your XML from this ...

<NOTARIO>
    <C_NOT>8404180</C_NOT>
    <APE>Abalos Nuevo</APE>
    <NOM>Francisco José</NOM>
    <NOTARIAS>
        <NOTARIA>
            <PRO>23</PRO>
            <MUN>0888</MUN>
            <F_IN>1984-12-01</F_IN>
            <F_FI>1986-09-19</F_FI>
        </NOTARIA>
        <NOTARIA>
            <PRO>14</PRO>
            <MUN>0569</MUN>
            <F_IN>1990-09-17</F_IN>
            <F_FI>1995-03-15</F_FI>
        </NOTARIA>
        <NOTARIA>
            <PRO>21</PRO>
            <MUN>0412</MUN>
            <F_IN>1995-03-30</F_IN>
            <F_FI></F_FI>
        </NOTARIA>
    </NOTARIAS>
</NOTARIO>

... into this:

<?xml version="1.0" encoding="UTF-8"?>
<dataroot>
    <NOTARIO>
        <C_NOT>8404180</C_NOT>
        <APE>Abalos Nuevo</APE>
        <NOM>Francisco José</NOM>
        <NOTARIA>
            <C_NOT>8404180</C_NOT>
            <PRO>23</PRO>
            <MUN>0888</MUN>
            <F_IN>1984-12-01</F_IN>
            <F_FI>1986-09-19</F_FI>
        </NOTARIA>
        <NOTARIA>
            <C_NOT>8404180</C_NOT>
            <PRO>14</PRO>
            <MUN>0569</MUN>
            <F_IN>1990-09-17</F_IN>
            <F_FI>1995-03-15</F_FI>
        </NOTARIA>
        <NOTARIA>
            <C_NOT>8404180</C_NOT>
            <PRO>21</PRO>
            <MUN>0412</MUN>
            <F_IN>1995-03-30</F_IN>
            <F_FI />
        </NOTARIA>
    </NOTARIO>
</dataroot>

... in the background while Access is importing it.

Save that XSLT file to your hard drive (I called mine "transformio.xslt"), then start the Access XML import process. Once you've selected your XML file to import, click the "Transform" button ...

ImportXmlDialog.png

... add your newly-created XSLT file to the list and select it ...

ImportTransforms.png

When you click "OK" and return to the "Import XML" dialog, you can expand the tree view to see that you now have C_NOT values in both tables.

ImportXml2.png

When the import is complete you will still have two tables, but now you can JOIN them on C_NOT to get a "flat" view of the data:

QueryDesign.png

which gives us

QuerySheet.png

like image 159
Gord Thompson Avatar answered Sep 26 '22 03:09

Gord Thompson