从名称空间的Oracle XMLTABLE获取XMLElement

时间:2017-03-09 16:16:21

标签: oracle namespaces xmltable

我的第一个问题,请原谅错误......

我有这个XML,来自加拿大银行,平均每日汇率

<?xml version="1.0" encoding="UTF-8"?>
<rdf:RDF    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
            xmlns="http://purl.org/rss/1.0/"
            xmlns:cb="http://www.cbwiki.net/wiki/index.php/Specification_1.1"
            xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel rdf:about="http://www.bankofcanada.ca/valet/fx_rss/FXUSDCAD">
    <title>Daily exchange rates</title>
    <link>http://www.bankofcanada.ca/?p=39898</link>
    <description>Daily average exchange rates - published once each business day by 16:30 ET. All Bank of Canada exchange rates are indicative rates only.</description>
    <items>
      <rdf:Seq>
        <rdf:li rdf:resource="http://www.bankofcanada.ca/valet/fx_rss/FXUSDCAD" />
      </rdf:Seq>
    </items>
    <dc:language>en</dc:language>
  </channel>
  <item rdf:about="http://www.bankofcanada.ca/valet/fx_rss/FXUSDCAD">
    <title>CA: 1.3475 CAD = 1 USD 2017-03-08</title>
    <link>http://www.bankofcanada.ca/?p=39898</link>
    <description>1 USD = 1.3475 CAD (US dollar to Canadian dollar daily exchange rate)</description>
    <dc:date>2017-03-08T21:30:00Z</dc:date>
    <dc:language>en</dc:language>
    <cb:statistics>
      <cb:country>CA</cb:country>
      <cb:exchangeRate>
        <cb:value decimals="4">1.3475</cb:value>
        <cb:baseCurrency>CAD</cb:baseCurrency>
        <cb:targetCurrency>USD</cb:targetCurrency>
        <cb:rateType>Bank of Canada exchange rate</cb:rateType>
        <cb:observationPeriod frequency="daily">2017-03-08T21:30:00Z</cb:observationPeriod>
      </cb:exchangeRate>
    </cb:statistics>
  </item>
</rdf:RDF>

我有这个Oracle PL / Sql来获取一些值,它返回一行,但它没有返回任何数据。所有列都是空白或空,我遗漏了一些东西,但我不确定是什么?我在Oracle 11g数据库中运行它。

with xml_data AS
(select xmltype(
'<?xml version="1.0" encoding="UTF-8"?>
<rdf:RDF    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
            xmlns="http://purl.org/rss/1.0/"
            xmlns:cb="http://www.cbwiki.net/wiki/index.php/Specification_1.1"
            xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel rdf:about="http://www.bankofcanada.ca/valet/fx_rss/FXUSDCAD">
    <title>Daily exchange rates</title>
    <link>http://www.bankofcanada.ca/?p=39898</link>
    <description>Daily average exchange rates - published once each business day by 16:30 ET. All Bank of Canada exchange rates are indicative rates only.</description>
    <items>
      <rdf:Seq>
        <rdf:li rdf:resource="http://www.bankofcanada.ca/valet/fx_rss/FXUSDCAD" />
      </rdf:Seq>
    </items>
    <dc:language>en</dc:language>
  </channel>
  <item rdf:about="http://www.bankofcanada.ca/valet/fx_rss/FXUSDCAD">
    <title>CA: 1.3475 CAD = 1 USD 2017-03-08</title>
    <link>http://www.bankofcanada.ca/?p=39898</link>
    <description>1 USD = 1.3475 CAD (US dollar to Canadian dollar daily exchange rate)</description>
    <dc:date>2017-03-08T21:30:00Z</dc:date>
    <dc:language>en</dc:language>
    <cb:statistics>
      <cb:country>CA</cb:country>
      <cb:exchangeRate>
        <cb:value decimals="4">1.3475</cb:value>
        <cb:baseCurrency>CAD</cb:baseCurrency>
        <cb:targetCurrency>USD</cb:targetCurrency>
        <cb:rateType>Bank of Canada exchange rate</cb:rateType>
        <cb:observationPeriod frequency="daily">2017-03-08T21:30:00Z</cb:observationPeriod>
      </cb:exchangeRate>
    </cb:statistics>
  </item>
</rdf:RDF>'
) xmlcol from dual
)
SELECT length(x1.xmlcol) as x_len, x2.title
from   xml_data  x1,
       XMLTABLE (
         XMLNAMESPACES (
           'http://www.w3.org/1999/02/22-rdf-syntax-ns#'            AS "rdf",
           'http://www.cbwiki.net/wiki/index.php/Specification_1.1' AS "cb",
           'http://purl.org/dc/elements/1.1/'                       AS "dc"),
         '/rdf:RDF'
         PASSING x1.XMLCOL
         COLUMNS 
             title  VARCHAR2 (1024) PATH '/channel/title'
         ) x2

1 个答案:

答案 0 :(得分:0)

我的XMLTable声明错过了&#39;默认&#39;命名空间parm,注意

默认&#39; http://purl.org/rss/1.0/&#39;,

XMLNAMESPACES区域中的

......

SELECT length(x1.xmlcol) as x_len, x2.*
from   xml_data  x1,
       XMLTABLE (
       XMLNAMESPACES 
         (  default 'http://purl.org/rss/1.0/',
            'http://www.w3.org/1999/02/22-rdf-syntax-ns#'             AS "rdf",
            'http://www.cbwiki.net/wiki/index.php/Specification_1.1'  AS "cb",
            'http://purl.org/dc/elements/1.1/'                        AS "dc"
         ),
         '/rdf:RDF/item/cb:statistics/cb:exchangeRate'
         PASSING x1.XMLCOL
         COLUMNS 
             title              VARCHAR2 (1024) PATH 'cb:value',
             observationPeriod  VARCHAR2 (1024) PATH 'cb:observationPeriod'
         ) x2