【问题标题】:Select XML element values and display multiple rows Oracle 11g选择 XML 元素值并显示多行 Oracle 11g
【发布时间】:2013-08-07 16:41:40
【问题描述】:

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

我一直在指出 Oracle API 的方向,但我不太擅长阅读 API,并且发现这个 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。

任何帮助将不胜感激。

【问题讨论】:

    标签: sql xml oracle11g plsqldeveloper xmltype


    【解决方案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;
    

    【讨论】:

      【解决方案2】:

      试试这个。可能有更简单的方法。

      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 表达式中。

      【讨论】:

      • 你看过 SQLFiddle 的例子了吗?我使用您提供的示例数据运行它。
      • 我做到了。我在下面发布的代码是我设法正常工作的唯一代码。
      猜你喜欢
      • 2012-04-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-18
      • 2015-08-08
      • 2014-12-10
      • 2013-12-01
      • 1970-01-01
      相关资源
      最近更新 更多