如何从Oracle数据库中提取嵌套的XMLType数据?

时间:2015-09-23 21:05:11

标签: oracle xmltype

我在数据库中的xmltype列中存储了以下xml数据。

<TRNXS_DTL transactionSeq="1">
  <TRNX_ITEM sequence="1" name="ERROR_CODE" value="2009"/>
  <TRNX_ITEM sequence="2" name="ERROR_DESC" value="Data Not Found for given record id."/>
</TRNXS_DTL>

现在我尝试使用extract查询数据库,如下所示:

  SELECT SRC_DSC_TXT as "TXT",   
  TYPE_CODE as "Code",   
  PID as "Participant ID",   
  SYS_ID as "System",   
  CHANEL as "Channel",   
  to_char(crt_ts,'MM/DD/YYYY HH24:MI:SS') as "Timestamp (MST)",   
  extract(TRX_DATA, '/TRNXS_DTL/TRNX_ITEM/text()') as "DETAILS"
  FROM PARTICIPANT_DATA,   
  WHERE SRC_VAL_TXT   =TYPE_CD;

但是当我运行上面的查询时,我在“详细信息”列下得到null,有什么想法吗?

1 个答案:

答案 0 :(得分:1)

XML中的标记没有任何文本。它有三个属性:序列,名称和值。 因此,您的查询返回NULL。

请考虑以下示例。

create table myt(
type_code number,
trx_data xmltype
);

insert into myt values(101,
'<TRNXS_DTL transactionSeq="1">
  <TRNX_ITEM sequence="1" name="ERROR_CODE" value="2009"/>
  <TRNX_ITEM sequence="2" name="ERROR_DESC" value="Data Not Found for given record id."/>
</TRNXS_DTL>'
);

insert into myt values(102,
'<TRNXS_DTL transactionSeq="1">
  <TRNX_ITEM sequence="1" name="ERROR_CODE" value="2010">NNN</TRNX_ITEM>
  <TRNX_ITEM sequence="2" name="ERROR_DESC" value="Data Found for given record id.">OOO</TRNX_ITEM>
</TRNXS_DTL>'
);

SQL> select  * from myt;       

 TYPE_CODE TRX_DATA
---------- -------------------------------------------------------------------------------------------------------------
       101 <TRNXS_DTL transactionSeq="1">
             <TRNX_ITEM sequence="1" name="ERROR_CODE" value="2009"/>
             <TRNX_ITEM sequence="2" name="ERROR_DESC" value="Data Not Found for given record id."/>
           </TRNXS_DTL>

       102 <TRNXS_DTL transactionSeq="1">
             <TRNX_ITEM sequence="1" name="ERROR_CODE" value="2010">NNN</TRNX_ITEM>
             <TRNX_ITEM sequence="2" name="ERROR_DESC" value="Data Found for given record id.">OOO</TRNX_ITEM>
           </TRNXS_DTL>
对于type_code 102,

将NNN和OOO作为文本。

SQL> select type_code,
extract(trx_data, '/TRNXS_DTL/TRNX_ITEM/text()').getStringVal() as "DETAILS"
from myt; 

 TYPE_CODE DETAILS
---------- -------------
       101
       102 NNNOOO

你可以看到,它连接了两个标签的文本。

如果您在属性值之后,可以使用@来指定属性。

SQL> select type_code,
extract(trx_data, '/TRNXS_DTL/TRNX_ITEM/@value').getStringVal() as "DETAILS"
from myt; 

 TYPE_CODE DETAILS
---------- ------------------------------------------
       101 2009Data Not Found for given record id.
       102 2010Data Found for given record id.

同样,正如您所看到的,文本是连接在一起的。

如果您希望它们位于不同的列中,则需要提供谓词以有条件地限制所选的节点。

SQL> select type_code,
extract(trx_data, '/TRNXS_DTL/TRNX_ITEM[@sequence="1"]/text()').getStringVal() as "DETAILS",
extract(trx_data, '/TRNXS_DTL/TRNX_ITEM[@sequence="2"]/text()').getStringVal() as "DETAILS2"
from myt;

 TYPE_CODE DETAILS                  DETAILS2
---------- ------------------------ --------------
       101
       102 NNN                      OOO


SQL> select type_code,
extract(trx_data, '/TRNXS_DTL/TRNX_ITEM[@sequence="1"]/@value').getStringVal() as "DETAILS",
extract(trx_data, '/TRNXS_DTL/TRNX_ITEM[@sequence="2"]/@value').getStringVal() as "DETAILS2"
from myt;

 TYPE_CODE DETAILS                  DETAILS2
---------- ------------------------ ------------------------------------------
       101 2009                     Data Not Found for given record id.
       102 2010                     Data Found for given record id.