想要从标签中提取文本

时间:2017-02-09 13:41:08

标签: oracle oracle10g oracle-sqldeveloper

我在表格中有一列,其中数据以下面的格式存储

  <Server start>
  <message tagid=" ">false</message>
  <message tagid="34">no data found</message>
  <message tagid=" ">false</message>
  <Server stop>

我需要提取的地方&#34;没有找到数据&#34;。尝试使用regexp替换功能,但无法以正确的方式使用。

注意:消息标记ID(编号)可以是任意长度。

2 个答案:

答案 0 :(得分:1)

<强>查询

SELECT x.tagid,
       x.message
FROM   your_table t
       CROSS JOIN
       XMLTable(
         '/Server/message'
         PASSING XMLType(
                   REPLACE(
                     REPLACE( t.your_column, '<Server start>', '<Server>' ),
                     '<Server stop>',
                     '</Server>'
                   )
                 )
           COLUMNS tagid   VARCHAR2(100) PATH './@tagid',
                   message VARCHAR2(400) PATH './text()'
       ) x;

<强>输出

TAGID MESSAGE
----- -------
      false
34    no data found
      false

如果您只想要消息为no data found的值,请在查询末尾添加WHERE子句:

WHERE x.message = 'no data found';

答案 1 :(得分:0)

尝试根据以下代码构建查询。首先将文本转换为XML,然后使用XMLType.extract()函数

提取必要的数据
with test_query as (select XMLType(replace(replace(s,'<Server stop>','</Server>'),'<Server start>','<Server>')) xml from
(select '<Server start>
  <message tagid=" ">false</message>
  <message tagid="34">no data found</message>
  <message tagid=" ">false</message>
  <Server stop>' as s from dual)) 
  select 
         t.xml.extract('//message[position()=2]/text()') out
   from test_query t;
相关问题