2013-08-06 58 views
0

我在Oracle 11g中見下表:選擇XML元素值並顯示多行的Oracle 11g

  CREATE TABLE jason_xml(
      id NUMBER(5) PRIMARY KEY, 
      xml_content XMLTYPE 
     )tablespace WD_T 

內xml_content列我有一個XML文檔:

 <results> 
<return> 
    <actualtime>0.0</actualtime> 
    <billingamount>0.0</billingamount> 
    <buildlisttext> 
     <buildnumber>0</buildnumber> 
     <completiondate>2007-04-10T12:36:00+02:00</completiondate> 
     <componentid>0</componentid> 
     <containsrecipients>false</containsrecipients> 
     <containsrecipientshasbeenset>true</containsrecipientshasbeenset> 
     <costamount>0.0</costamount> 
     <createdate>2006-11-20T17:10:02+01:00</createdate> 
     <createdbysystemuserid>89198</createdbysystemuserid> 
     <currentownersystemuserid>12122</currentownersystemuserid> 
     <currentownerusergroupid>0</currentownerusergroupid> 
     <customerid>95</customerid> 
     <description>From: Ricky Bolton</description> 
    </buildlisttext> 
</return> 
<return> 
    <actualtime>0.0</actualtime> 
    <billingamount>0.0</billingamount> 
    <buildlisttext> 
     <buildnumber>0</buildnumber> 
     <completiondate>2007-04-10T12:36:00+02:00</completiondate> 
     <componentid>0</componentid> 
     <containsrecipients>false</containsrecipients> 
     <containsrecipientshasbeenset>true</containsrecipientshasbeenset> 
     <costamount>0.0</costamount> 
     <createdate>2006-11-20T17:10:02+01:00</createdate> 
     <createdbysystemuserid>89198</createdbysystemuserid> 
     <currentownersystemuserid>12122</currentownersystemuserid> 
     <currentownerusergroupid>0</currentownerusergroupid> 
     <customerid>95</customerid> 
     <description>From: Derek Trotter</description> 
    </buildlisttext> 
</return> 
    </results> 

我想從我的jason_xml表格列中查詢此文檔,然後將結果顯示爲:

 |billingamount|Description| 
     |0.0   |From: Ricky Bolton| 
     |0.0   |From: Derek Trotter| 

我已經指出但是我不太擅長閱讀API,並且發現這篇文章寫得非常糟糕。我已經嘗試了一些此頁上定義的運營商,但有沒有喜悅:

http://docs.oracle.com/cd/B28359_01/appdev.111/b28369/xdb04cre.htm#BABDGFFH

我就這樣得到了,但不斷收到「無效的標識符」在PL/SQL開發人員。我知道我可能會完全錯誤,所以有人有任何指針/解決方案?

 SELECT extractValue(OBJECT_VALUE, 'results/return/buildlisttext/description') "DESCRIPTION" FROM jason_xml x WHERE xmlexists('results/return/buildlisttext/description' PASSING OBJECT_VALUE); 

我使用PHP與MySQL用戶和可以很容易地做到這一點與技術,結合可惜我不得不使用的Oracle 11g的工作。

任何幫助將不勝感激。

回答

1

事實證明,這是造成這種情況的正確的響應:

   SELECT xtab.billingamount, xtab.description 
       FROM jason_xml jx, xmltable('/results/return' 
       PASSING jx.xml_content 
       COLUMNS billingamount varchar2(4000) path '//billingAmount', 
         description clob path '//description' 
        )xtab; 
0

試試看。可能有一個更簡單的方法。

SELECT EXTRACTVALUE (t.COLUMN_VALUE, 'return/billingamount') Billing_Amount, 
     EXTRACTVALUE (t.COLUMN_VALUE, 'return/buildlisttext/description') Description 
    FROM jason_xml, 
     XMLTABLE ('for $i in /results/return return $i' 
       PASSING jason_xml.xml_content) t 

這裏是SQL Fiddle example

這不包括您展示的限制,但你可以很容易地添加到XQuery表達式。

+0

這將返回81個空行... – jezzipin

+0

你看SQLFiddle例子嗎?我用你提供的樣本數據運行它。 – OldProgrammer

+0

我做到了。我在下面發佈的代碼是我設法正確工作的唯一代碼。 – jezzipin