通过XSL转换将XML导入Access [英] Importing XML into Access with an XSL transform

查看:63
本文介绍了通过XSL转换将XML导入Access的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我还看到了其他一些类似答案的答案,但是我无法完全解决.我有一个需要导入到Access的以属性为中心的XML文件,该文件仅接受以元素为中心的格式.似乎我需要对XSL文件进行转换,但是我不清楚如何进行转换.由于数据是专有数据,因此我以科幻为主题对其进行了伪装.我需要做的是将第一个代码示例转换为与第二个示例相同的格式:

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!

推荐答案

尝试此XSLT.根据您的转换需求定义匹配RESOURCEDATAPLANETARYtemplates:

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>

第三个模板<xsl:template match="/PLANETARY">创建一个Protocol元素,添加@Protocol值,并将模板应用于其节点(即,在本例中,将模板应用于其子元素).

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).

第四张模板<xsl:template match="RESOURCEDATA">创建一个名称为@resource值和@value中值的元素.

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

来到前两个模板,将第一个(<xsl:template match="@*" >)应用于任何属性会将其转换为一个元素(以元素为中心).

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

第二个模板<xsl:template match="@*" >匹配节点(在您的情况下为元素),复制标签,并将模板应用于其属性和子元素(以递归方式).

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).

这篇关于通过XSL转换将XML导入Access的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆