2013-03-04 71 views
1

这与以前的帖子有关。我正在尝试将StLouisFred的经济数据导入到Access中。我将最终使用这个数据库的Excel。我是一名学生,这项任务虽然简单,但超出了我的经验。我也尝试将XML数据导入到Excel中,但格式仍然是一个问题。我知道FRED使用CSV,但不能自动更新CSV,所以我想使用XML。数据的格式如下:将XML格式转换为与Access导入兼容

<observations realtime_start="2013-02-08" realtime_end="2013-02-08" 
observation_start="1776-07-04" observation_end="9999-12-31" units="lin" 
output_type="2" file_type="xml" order_by="observation_date" sort_order="asc" 
count="792" offset="0"  limit="100000"> 
<observation date="1947-01-01" CPIAUCSL_20130208="21.48"/> 
<observation date="1947-02-01" CPIAUCSL_20130208="21.62"/> 
<observation date="1947-03-01" CPIAUCSL_20130208="22.0"/> 
</observations> 

我想将数据转换为Access首选的其他标准xml格式。这样的事情:

<observations realtime_start="2013-02-08" realtime_end="2013-02-08" 
observation_start="1776-07-04" observation_end="9999-12-31" units="lin" 
output_type="2" file_type="xml" order_by="observation_date" sort_order="asc" 
count="792" offset="0"  limit="100000"> 

<observation> 
<date> 1947-01-01 </date> 
<value> 21.48 </value> 
<observation/> 

<observation> 
<date>1947-02-01</date> 
<value>21.62</value> 
</observation> 

<observation> 
<date>1947-03-01</date> <value>22.0</value> 
</observation> 

</observations> 

看来,这将工作。 Access有能力使用样式表,我很乐意尝试,但我需要轻微的演练。因此,假设我有一张第一个xml格式的信息。有没有一种方法可以将数据转换为Access,以便我可以拥有一个将自动更新的表,或者这是一个无望的项目?

+0

看起来您希望将节点的属性转换为节点的子节点,并在此过程中重命名其中一个节点。这样做的标准方式是使用XSLT,因此我已经为该问题添加了该标记 – barrowc 2013-03-04 22:30:01

回答

2

下面是一个简单的XSLT变换:

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

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

<xsl:template match="observation/@date"> 
    <date><xsl:value-of select="."/></date> 
</xsl:template> 

<xsl:template match="observation/@*[starts-with(name(),'CPIAUCSL_')]"> 
    <value><xsl:value-of select="."/></value> 
</xsl:template> 
</xsl:stylesheet> 

并且当该变换被应用所提供的XML文档:

<observations realtime_start="2013-02-08" 
    realtime_end="2013-02-08" 
    observation_start="1776-07-04" 
    observation_end="9999-12-31" units="lin" 
    output_type="2" file_type="xml" 
    order_by="observation_date" sort_order="asc" 
    count="792" offset="0"  limit="100000"> 
    <observation date="1947-01-01" CPIAUCSL_20130208="21.48"/> 
    <observation date="1947-02-01" CPIAUCSL_20130208="21.62"/> 
    <observation date="1947-03-01" CPIAUCSL_20130208="22.0"/> 
</observations> 

有用,正确的结果产生:

<observations realtime_start="2013-02-08" 
realtime_end="2013-02-08" observation_start="1776-07-04" 
observation_end="9999-12-31" units="lin" output_type="2" 
file_type="xml" order_by="observation_date" sort_order="asc" 
count="792" offset="0" limit="100000"> 
    <observation> 
     <date>1947-01-01</date> 
     <value>21.48</value> 
    </observation> 
    <observation> 
     <date>1947-02-01</date> 
     <value>21.62</value> 
    </observation> 
    <observation> 
     <date>1947-03-01</date> 
     <value>22.0</value> 
    </observation> 
</observations>