在Pl中提取ValueXml

时间:2018-07-19 10:34:50

标签: oracle

我已经提取了
    EXTRACTVALUE(v_xml,'/ rss / channel / item / title')
 “ 2018-07-19”

我要提取所有“描述”值

<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
<title>RSS NBG Currency Rates</title>
<link>https://www.nbg.gov.ge/index.php?m=236&amp;lang=geo</link>
<description>Currency Rates</description>
<pubDate>Thu, 19 Jul 2018 14:10:50 +0400</pubDate>
<lastBuildDate>Thu, 19 Jul 2018 14:10:50 +0400</lastBuildDate>
<managingEditor>alex@proservice.ge</managingEditor>
<webMaster>alex@proservice.ge</webMaster>
<item>
     <title>2018-07-19</title>
 <link>https://www.nbg.gov.ge/index.php?m=236&amp;lang=geo</link>
 <description><![CDATA[<table border="0"><tr>
            <td>AED</td>
            <td>6.6828</td>
            <td>0.0158</td>
        </tr><tr>
            <td>ZAR</td>
            <td>1.8412</td>
            <td>0.0032</td>
        </tr></table>]]></description>
 <pubDate>Thu, 19 Jul 2018 14:10:50 +0400</pubDate>
 <guid>https://</guid>
</item>
</channel></rss>;

1 个答案:

答案 0 :(得分:1)

不推荐使用extractvalue函数。您可以改用单独的XMLQuery调用,也可以通过XMLTable获得标题和描述:

select *
from xmltable('/rss/channel/item'
  passing xmltype('<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
<title>RSS NBG Currency Rates</title>
<link>https://www.nbg.gov.ge/index.php?m=236&amp;lang=geo</link>
<description>Currency Rates</description>
<pubDate>Thu, 19 Jul 2018 14:10:50 +0400</pubDate>
<lastBuildDate>Thu, 19 Jul 2018 14:10:50 +0400</lastBuildDate>
<managingEditor>alex@proservice.ge</managingEditor>
<webMaster>alex@proservice.ge</webMaster>
<item>
     <title>2018-07-19</title>
 <link>https://www.nbg.gov.ge/index.php?m=236&amp;lang=geo</link>
 <description><![CDATA[<table border="0"><tr>
            <td>AED</td>
            <td>6.6828</td>
            <td>0.0158</td>
        </tr><tr>
            <td>ZAR</td>
            <td>1.8412</td>
            <td>0.0032</td>
        </tr></table>]]></description>
 <pubDate>Thu, 19 Jul 2018 14:10:50 +0400</pubDate>
 <guid>https://</guid>
</item>
</channel></rss>')
  columns title varchar2(30) path 'title',
    description varchar2(4000) path 'description'
);

得到

TITLE                          DESCRIPTION                                       
------------------------------ --------------------------------------------------
2018-07-19                     <table border="0"><tr>                            
                                           <td>AED</td>                          
                                           <td>6.6828</td>                       
                                           <td>0.0158</td>                       
                                       </tr><tr>                                 
                                           <td>ZAR</td>                          
                                           <td>1.8412</td>                       
                                           <td>0.0032</td>                       
                                       </tr></table>  

这会将XML作为字符串文字传递,如果您已经将其作为XMLType传递,则不需要转换xmltype('<string>'),因此如果v_xml是PL / SQL XMLType变量,则:

select title, description
into v_title, v_description -- use your own PL/SQL variable names
from xmltable('/rss/channel/item'
  passing v_xml
  columns title varchar2(30) path 'title',
    description varchar2(200) path 'description'
);

如果它是字符串(varchar2CLOB),则仍然需要转换它:

select title, description
into v_title, v_description -- use your own PL/SQL variable names
from xmltable('/rss/channel/item'
  passing xmltype(v_xml)
  columns title varchar2(30) path 'title',
    description varchar2(200) path 'description'
);

您可以通过向columns子句添加更多条目来提取其他数据。


如果-这是一个很大的'if'-CDATA部分将始终采用相同的格式,格式正确的CDATA和HTML带有一个包含三列的表格,那么您可以将HTML表格视为XML(常常是不安全的),然后使用另一个XMLTable提取单个值:

select x1.title, x2.col1, x2.col2, x2.col3
from xmltable('/rss/channel/item'
  passing xmltype('<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
<title>RSS NBG Currency Rates</title>
<link>https://www.nbg.gov.ge/index.php?m=236&amp;lang=geo</link>
<description>Currency Rates</description>
<pubDate>Thu, 19 Jul 2018 14:10:50 +0400</pubDate>
<lastBuildDate>Thu, 19 Jul 2018 14:10:50 +0400</lastBuildDate>
<managingEditor>alex@proservice.ge</managingEditor>
<webMaster>alex@proservice.ge</webMaster>
<item>
     <title>2018-07-19</title>
 <link>https://www.nbg.gov.ge/index.php?m=236&amp;lang=geo</link>
 <description><![CDATA[<table border="0"><tr>
            <td>AED</td>
            <td>6.6828</td>
            <td>0.0158</td>
        </tr><tr>
            <td>ZAR</td>
            <td>1.8412</td>
            <td>0.0032</td>
        </tr></table>]]></description>
 <pubDate>Thu, 19 Jul 2018 14:10:50 +0400</pubDate>
 <guid>https://</guid>
</item>
</channel></rss>')
  columns title varchar2(30) path 'title',
    description clob path 'description'
) x1
cross join xmltable('/table/tr'
  passing xmltype(x1.description)
  columns col1 varchar2(10) path 'td[1]',
    col2 varchar2(20) path 'td[2]',
    col3 varchar2(20) path 'td[3]'
) x2;

得到:

TITLE                          COL1       COL2                 COL3                
------------------------------ ---------- -------------------- --------------------
2018-07-19                     AED        6.6828               0.0158              
2018-07-19                     ZAR        1.8412               0.0032              

请注意,尽管HTML <> XML,但是通常您不能像这样解析HTML。尽管如此,还有其他收费。