Importing XML into Access with an XSL transform

我的未来我决定 提交于 2019-12-11 13:44:01

问题


I've seen a few other answers for similar answers, but I can't quite wrap my head around it. I have an attribute centric XML file that I need to import into Access, which will only accept element centric formatting. It seems like I need to do a transform with an XSL file, but I'm not clear how to do it. Since the data is proprietary, I've disguised it with a sci fi theme. What I need to do is turn the first code example into the same format as the second one:

<PLANETARY Protocol="Solar 1">
  <COLONIES>
        <COLONYDATA site="10001" planet="Mars">
            <RESOURCEDATA resource="RadiationDanger" value="Low" />
            <RESOURCEDATA resource="ApplicantColonists" value="11" />
            <RESOURCEDATA resource="AcceptedColonists" value="3" />
        </COLONYDATA>
        <COLONYDATA site="10002" planet="Mars">
            <RESOURCEDATA resource="RadiationDanger" value="Low" />
            <RESOURCEDATA resource="ApplicantColonists" value="7" />
            <RESOURCEDATA resource="AcceptedColonists" value="1" />
        </COLONYDATA>
        <COLONYDATA site="11019" planet="Titan">
            <RESOURCEDATA resource="RadiationDanger" value="Low" />
            <RESOURCEDATA resource="ApplicantColonists" value="22" />
            <RESOURCEDATA resource="AcceptedColonists" value="16" />
        </COLONYDATA>
    </COLONIES>
  </PLANETARY>


<Protocol>
Solar1
    <COLONIES>
        <COLONYDATA>
            <site>10001</site>
            <planet>Mars</planet>
            <RadiationDanger>Low</RadiationDanger>
            <ApplicantColonists>11</ApplicantColonists>
            <AcceptedColonists>3</AcceptedColonists>
        </COLONYDATA>
        <COLONYDATA>
            <site>10002</site>
            <planet>Mars</planet>
            <RadiationDanger>Low</RadiationDanger>
            <ApplicantColonists>7</ApplicantColonists>
            <AcceptedColonists>1</AcceptedColonists>
        </COLONYDATA>
        <COLONYDATA>
            <site>11019</site>
            <planet>Titan</planet>
            <RadiationDanger>Low</RadiationDanger>
            <ApplicantColonists>22</ApplicantColonists>
            <AcceptedColonists>16</AcceptedColonists>
        </COLONYDATA>
    </COLONIES>
</Protocol>

Any help would be appreciated. Thanks!


回答1:


Try this XSLT. templates matching RESOURCEDATA and PLANETARY are defined as per your transformation needs:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" indent="yes"/>
<xsl:strip-space elements="*"/>

<xsl:template match="@*" >
    <xsl:element name="{name()}">
        <xsl:value-of select="."/>
    </xsl:element>
</xsl:template>

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

<xsl:template match="/PLANETARY">
    <Protocol>
        <xsl:value-of select="@Protocol"/>
        <xsl:apply-templates select="node()"/>
    </Protocol>
</xsl:template>

<xsl:template match="RESOURCEDATA">
    <xsl:element name="{@resource}">
        <xsl:value-of select="@value"/>
    </xsl:element>
</xsl:template>

</xsl:stylesheet>

The third template <xsl:template match="/PLANETARY"> creates a Protocol element, adds @Protocol value, and applies templates to its nodes(i.e., in this case, applying templates to its child elements).

The fouth template <xsl:template match="RESOURCEDATA"> creates an element with name as @resource's value, and value from @value.

Coming to the first two templates, first one (<xsl:template match="@*" >) when applied to any attribute converts it into an element(element-centric).

And the second template, <xsl:template match="@*" > matches nodes(in your case elements), copy the tags, and apply templates for its attribtues and child elements(in a recursive way).



来源:https://stackoverflow.com/questions/26835619/importing-xml-into-access-with-an-xsl-transform

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!