2013-03-04 74 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>