0

试图解析来自 treasury.gov 的 XML,它使用两个命名空间。但是,我似乎无法从 XML 中提取数据。我的查询没有返回任何结果。我尝试将命名空间添加到preparedoc,但没有返回任何数据。命名空间是问题所在,就好像我删除它们一样,我的代码可以工作。作为一种解决方法,使用替换来消除命名空间以“清理”XML。这完全消除了命名空间,直接呈现 XML。虽然这可行,但它非常难看,显然不是解决问题的正确方法。

    DECLARE @xml AS XML;
    DECLARE @idoc INT;

SET @xml = '
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<feed xml:base="http://data.treasury.gov/Feed.svc/" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xmlns="http://www.w3.org/2005/Atom">
  <title type="text">DailyTreasuryYieldCurveRateData</title>
  <id>http://data.treasury.gov/feed.svc/DailyTreasuryYieldCurveRateData</id>
  <updated>2019-05-30T20:27:58Z</updated>
  <link rel="self" title="DailyTreasuryYieldCurveRateData" href="DailyTreasuryYieldCurveRateData" />
  <entry>
    <id>http://data.treasury.gov/Feed.svc/DailyTreasuryYieldCurveRateData(1)</id>
    <title type="text"></title>
    <updated>2019-05-30T20:27:58Z</updated>
    <author>
      <name />
    </author>
    <link rel="edit" title="DailyTreasuryYieldCurveRateDatum" href="DailyTreasuryYieldCurveRateData(1)" />
    <category term="TreasuryDataWarehouseModel.DailyTreasuryYieldCurveRateDatum" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" />
    <content type="application/xml">
      <m:properties>
        <d:Id m:type="Edm.Int32">1</d:Id>
        <d:NEW_DATE m:type="Edm.DateTime">1997-01-02T00:00:00</d:NEW_DATE>
        <d:BC_1MONTH m:type="Edm.Double" m:null="true" />
        <d:BC_2MONTH m:type="Edm.Double" m:null="true" />
        <d:BC_3MONTH m:type="Edm.Double">5.190000057220459</d:BC_3MONTH>
        <d:BC_6MONTH m:type="Edm.Double">5.3499999046325684</d:BC_6MONTH>
        <d:BC_1YEAR m:type="Edm.Double">5.630000114440918</d:BC_1YEAR>
        <d:BC_2YEAR m:type="Edm.Double">5.96999979019165</d:BC_2YEAR>
        <d:BC_3YEAR m:type="Edm.Double">6.130000114440918</d:BC_3YEAR>
        <d:BC_5YEAR m:type="Edm.Double">6.3000001907348633</d:BC_5YEAR>
        <d:BC_7YEAR m:type="Edm.Double">6.4499998092651367</d:BC_7YEAR>
        <d:BC_10YEAR m:type="Edm.Double">6.5399999618530273</d:BC_10YEAR>
        <d:BC_20YEAR m:type="Edm.Double">6.8499999046325684</d:BC_20YEAR>
        <d:BC_30YEAR m:type="Edm.Double">6.75</d:BC_30YEAR>
        <d:BC_30YEARDISPLAY m:type="Edm.Double">0</d:BC_30YEARDISPLAY>
      </m:properties>
    </content>
  </entry>
</feed>
'

EXEC sp_XML_PrepareDocument @iDoc OUTPUT, @xml, '<feed xml:base="http://data.treasury.gov/Feed.svc/" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xmlns:x="http://www.w3.org/2005/Atom">';

SELECT *
FROM OPENXML(@iDoc, 'feed/entry/content/m:properties/' )
WITH
(
   [Id] VARCHAR(100) 'd:Id'                                                                                             
  ,[Date] VARCHAR(100) 'd:NEW_DATE'
)


EXEC sp_xml_removedocument @iDoc

Id 和 Date 列不返回任何数据。如果我消除命名空间,它将按预期返回数据。

4

1 回答 1

0

FROM OPENXML存储过程一起准备和删除文档的方法已经过时,不应再使用。而是使用本机 XML 方法:

你的 XML

DECLARE @xml XML =
'<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<feed xml:base="http://data.treasury.gov/Feed.svc/" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xmlns="http://www.w3.org/2005/Atom">
  <title type="text">DailyTreasuryYieldCurveRateData</title>
  <id>http://data.treasury.gov/feed.svc/DailyTreasuryYieldCurveRateData</id>
  <updated>2019-05-30T20:27:58Z</updated>
  <link rel="self" title="DailyTreasuryYieldCurveRateData" href="DailyTreasuryYieldCurveRateData" />
  <entry>
    <id>http://data.treasury.gov/Feed.svc/DailyTreasuryYieldCurveRateData(1)</id>
    <title type="text"></title>
    <updated>2019-05-30T20:27:58Z</updated>
    <author>
      <name />
    </author>
    <link rel="edit" title="DailyTreasuryYieldCurveRateDatum" href="DailyTreasuryYieldCurveRateData(1)" />
    <category term="TreasuryDataWarehouseModel.DailyTreasuryYieldCurveRateDatum" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" />
    <content type="application/xml">
      <m:properties>
        <d:Id m:type="Edm.Int32">1</d:Id>
        <d:NEW_DATE m:type="Edm.DateTime">1997-01-02T00:00:00</d:NEW_DATE>
        <d:BC_1MONTH m:type="Edm.Double" m:null="true" />
        <d:BC_2MONTH m:type="Edm.Double" m:null="true" />
        <d:BC_3MONTH m:type="Edm.Double">5.190000057220459</d:BC_3MONTH>
        <d:BC_6MONTH m:type="Edm.Double">5.3499999046325684</d:BC_6MONTH>
        <d:BC_1YEAR m:type="Edm.Double">5.630000114440918</d:BC_1YEAR>
        <d:BC_2YEAR m:type="Edm.Double">5.96999979019165</d:BC_2YEAR>
        <d:BC_3YEAR m:type="Edm.Double">6.130000114440918</d:BC_3YEAR>
        <d:BC_5YEAR m:type="Edm.Double">6.3000001907348633</d:BC_5YEAR>
        <d:BC_7YEAR m:type="Edm.Double">6.4499998092651367</d:BC_7YEAR>
        <d:BC_10YEAR m:type="Edm.Double">6.5399999618530273</d:BC_10YEAR>
        <d:BC_20YEAR m:type="Edm.Double">6.8499999046325684</d:BC_20YEAR>
        <d:BC_30YEAR m:type="Edm.Double">6.75</d:BC_30YEAR>
        <d:BC_30YEARDISPLAY m:type="Edm.Double">0</d:BC_30YEARDISPLAY>
      </m:properties>
    </content>
  </entry>
</feed>';

--涉及多个命名空间。一个xmlns是所谓的默认命名空间,而其他命名空间有一个前缀。您可以使用任何您喜欢的前缀,但是 - 为了便于阅读 - 我使用与原始 XML 相同的前缀:

--下面的代码将演示,如何直接读取一些顶级元素,如何使用.nodes()潜入嵌套元素以及如何使用另一个.nodes()潜入相关子节点。

WITH XMLNAMESPACES(DEFAULT 'http://www.w3.org/2005/Atom'
                          ,'http://schemas.microsoft.com/ado/2007/08/dataservices/metadata' AS m
                          ,'http://schemas.microsoft.com/ado/2007/08/dataservices' AS d
                          ,'http://data.treasury.gov/Feed.svc/' AS base )
SELECT @xml.value('(/feed/title/text())[1]','varchar(100)') AS title
      ,@xml.value('(/feed/title/@type)[1]','varchar(100)') AS title_type
      ,@xml.value('(/feed/id/text())[1]','varchar(100)') AS id
      ,@xml.value('(/feed/updated/text())[1]','datetime') AS updated --type-safe
      --pick more elements in top level
      ,ent.value('(id/text())[1]','varchar(100)') AS entry_id
      --pick more elements from entry-level
      ,prp.value('(d:Id/text())[1]','int') AS Prop_id
      --pick more elements below <m:properties>
FROM @xml.nodes('/feed/entry') A(ent)
CROSS APPLY A.ent.nodes('content/m:properties') B(prp);

--如果属性可能事先不知道,您可以*在路径中使用并返回通用 EAV 列表:

WITH XMLNAMESPACES(DEFAULT 'http://www.w3.org/2005/Atom'
                          ,'http://schemas.microsoft.com/ado/2007/08/dataservices/metadata' AS m
                          ,'http://schemas.microsoft.com/ado/2007/08/dataservices' AS d
                          ,'http://data.treasury.gov/Feed.svc/' AS base )
SELECT @xml.value('(/feed/title/text())[1]','varchar(100)') AS title
      ,@xml.value('(/feed/title/@type)[1]','varchar(100)') AS title_type
      ,@xml.value('(/feed/id/text())[1]','varchar(100)') AS id
      ,@xml.value('(/feed/updated/text())[1]','datetime') AS updated --type-safe
      --pick more elements in top level
      ,ent.value('(id/text())[1]','varchar(100)') AS entry_id
      --pick more elements from entry-level
      ,AllPrps.value('local-name(.)','varchar(100)') AS Prop_Name
      ,AllPrps.value('@m:type','varchar(100)') AS Prop_Value
      ,AllPrps.value('text()[1]','varchar(100)') AS Prop_Value
      --pick more elements below <m:properties>
FROM @xml.nodes('/feed/entry') A(ent)
CROSS APPLY A.ent.nodes('content/m:properties/d:*') B(AllPrps)
于 2019-07-10T14:18:09.467 回答