简体   繁体   中英

Transforming Yahoo Finance Currency XML in order to import into Access with XSLT

I am trying to create an XSLT file that I can use to properly import the Yahoo all currency feed at http://finance.yahoo.com/webservice/v1/symbols/allcurrencies/quote?format . I want to eventually build this into VBA to automatically import.

The xml file looks like below (but has many more currencies):

<list version="1.0">
<meta>
    <type>resource-list</type>
</meta>
<resources start="0" count="174">
    <resource classname="Quote">
        <field name="name">USD/KRW</field>
        <field name="price">1174.170044</field>
        <field name="symbol">KRW=X</field>
        <field name="ts">1484857724</field>
        <field name="type">currency</field>
        <field name="utctime">2017-01-19T20:28:44+0000</field>
        <field name="volume">0</field>
    </resource><resource classname="Quote">
        <field name="name">SILVER 1 OZ 999 NY</field>
        <field name="price">0.053778</field>
        <field name="symbol">XAG=X</field>
        <field name="ts">1484857681</field>
        <field name="type">currency</field>
        <field name="utctime">2017-01-19T20:28:01+0000</field>
        <field name="volume">36</field>
    </resource></resources>
</list>
<!-- iapi4.finance.bf1.yahoo.com Thu Jan 19 15:30:54 EST 2017 -->

I have tried creating a XSLT file but I think it is completely wrong. I have looked at some different examples but I could not figure out how to customize for the provided file. I do believe a lot of users would be interested in using this file and that it could be re-purposed for many different people.

Shamelessly stealing from Parfait's excellent answer here , I came up with the XSLT file

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

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

  <xsl:template match="meta">
    <!-- omit -->
  </xsl:template>

  <xsl:template match="resource">
    <xsl:copy>
      <xsl:for-each select="*">
        <xsl:if test="@*">
            <xsl:element name="{@*}"><xsl:value-of select="."/></xsl:element>
          </xsl:if>
      </xsl:for-each>
    </xsl:copy>
  </xsl:template>

</xsl:stylesheet>

that will transform the source XML file to

<?xml version="1.0" encoding="UTF-16"?>
<resources start="0" count="174">
    <resource>
        <name>USD/KRW</name>
        <price>1174.170044</price>
        <symbol>KRW=X</symbol>
        <ts>1484857724</ts>
        <type>currency</type>
        <utctime>2017-01-19T20:28:44+0000</utctime>
        <volume>0</volume>
    </resource>
    <resource>
        <name>SILVER 1 OZ 999 NY</name>
        <price>0.053778</price>
        <symbol>XAG=X</symbol>
        <ts>1484857681</ts>
        <type>currency</type>
        <utctime>2017-01-19T20:28:01+0000</utctime>
        <volume>36</volume>
    </resource>
</resources>
<!-- iapi4.finance.bf1.yahoo.com Thu Jan 19 15:30:54 EST 2017 -->

which Access can import into a table named [resource].

Turn on the Macro Recorder and go to Data > From Web > Import the link you posted above. I just did that and got the code below.

Sub Macro1()

    ActiveWorkbook.XmlImport URL:= _
        "http://finance.yahoo.com/webservice/v1/symbols/allcurrencies/quote?format", _
        ImportMap:=Nothing, Overwrite:=True, Destination:=Range("$A$1")
End Sub

Here is the final result.

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM