【问题标题】:Transforming Yahoo Finance Currency XML in order to import into Access with XSLT转换 Yahoo Finance Currency XML 以便使用 XSLT 导入 Access
【发布时间】:2017-06-05 21:22:37
【问题描述】:

我正在尝试创建一个 XSLT 文件,我可以使用它来正确导入位于 http://finance.yahoo.com/webservice/v1/symbols/allcurrencies/quote?format 的 Yahoo 所有货币提要。 我想最终将其构建到 VBA 中以自动导入。

xml 文件如下所示(但包含更多货币):

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

我曾尝试创建一个 XSLT 文件,但我认为这是完全错误的。我查看了一些不同的示例,但我无法弄清楚如何为提供的文件进行自定义。我相信很多用户都会对使用此文件感兴趣,并且它可以重新用于许多不同的人。

【问题讨论】:

  • 我认为目前尚不清楚您在问什么。首先,您应该包含您的 XSLT 文件。在那之后,你看过哪些例子(如果它们是相关的)? XSLT 转换的预期输出是什么? (.csv 文件?不同的 XML 格式?...)

标签: xml vba ms-access xslt


【解决方案1】:

无耻地从 Parfait 的优秀答案 here 中窃取,我想出了 XSLT 文件

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

这会将源 XML 文件转换为

<?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 -->

哪些 Access 可以导入到名为 [resource] 的表中。

【讨论】:

  • 哈哈...这里不丢人!我们应该注意到,Access 的导入方法是以元素为中心的,而不是以属性为中心的,因为这个 XML 用列名显示。
  • 非常感谢这一切都很好 - 感谢 Gord 和 Thompson
【解决方案2】:

打开宏记录器并转到数据 > 从 Web > 导入您在上面发布的链接。我只是这样做并得到了下面的代码。

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

这是最终结果。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-05-09
    • 2018-01-02
    • 1970-01-01
    • 2011-08-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多