从存储为CLOB的XML中提取数据

时间:2016-06-20 20:45:43

标签: xml oracle clob

我需要帮助提取存储在CLOB列中的数据。我想将CLOB数据转换为结构化的列和行。

CLOB列数据:

<questions version="1">
<fld code="ADV" vis="Y"><oriVal></oriVal><curVal>N</curVal></fld>
<fld code="ASEN1" vis="Y"><oriVal></oriVal><curVal>N</curVal></fld>
<fld code="COGN" vis="Y"><oriVal></oriVal><curVal>A</curVal></fld>
<fld code="`enter code here`COM" vis="Y"><oriVal></oriVal></fld>
<fld code="COMORBID" vis="Y"><oriVal></oriVal><curVal>1</curVal></fld>
<fld code="DIET" vis="Y"><oriVal></oriVal><curVal>R</curVal></fld>
<fld code="DIS" vis="Y"><oriVal></oriVal></fld>
<fld code="DISC" vis="Y"><oriVal></oriVal><curVal></curVal></fld>
<fld code="DISC1" vis="Y"><oriVal></oriVal><curVal></curVal></fld>
<fld code="DISC2" vis="Y"><oriVal></oriVal><curVal></curVal></fld>
<fld code="DNR" vis="Y"><oriVal></oriVal><curVal>N</curVal></fld>
<fld code="ER" vis="Y"><oriVal></oriVal><curVal>1</curVal></fld>
</questions>    

我希望将数据视为

CODE        VALUE (curVal)
----------  --------------
ADV         N
ASEN1       N
COGN        Y
COMORBID    1

我尝试运行多个查询,但无法达到预期效果。

SELECT EXTRACTVALUE(xmltype(QUESTIONXML), '/questions/fld/oriVal/curVal[@code="ADV"]') 
FROM pac_copy.questionnaire;

SELECT EXTRACTVALUE(QUESTIONXML, '/questions/fld') FROM pac_copy.questionnaire;

SELECT XMLTYPE(u.QUESTIONXML).EXTRACT('/questions/fld/text()') as SORTCODE 
FROM pac_copy.questionnaire  u;

1 个答案:

答案 0 :(得分:0)

您可以使用XMLTable()

select x.code, x.curval
from questionnaire q
cross join xmltable('/questions/fld'
  passing xmltype(q.questionxml)
  columns code varchar2(10) path '@code',
    curval varchar2(10) path '/curVal'
) x;

CODE       CURVAL   
---------- ----------
ADV        N         
ASEN1      N         
COGN       A         
COM                  
COMORBID   1         
DIET       R         
DIS                  
DISC                 
DISC1                
DISC2                
DNR        N         
ER         1         

您可以过滤是否要限制某些代码,或仅限制设置值的代码;使用CTE提供字符串值:

with questionnaire (questionxml) as (
  select '<questions version="1">
<fld code="ADV" vis="Y"><oriVal></oriVal><curVal>N</curVal></fld>
<fld code="ASEN1" vis="Y"><oriVal></oriVal><curVal>N</curVal></fld>
<fld code="COGN" vis="Y"><oriVal></oriVal><curVal>A</curVal></fld>
<fld code="COM" vis="Y"><oriVal></oriVal></fld>
<fld code="COMORBID" vis="Y"><oriVal></oriVal><curVal>1</curVal></fld>
<fld code="DIET" vis="Y"><oriVal></oriVal><curVal>R</curVal></fld>
<fld code="DIS" vis="Y"><oriVal></oriVal></fld>
<fld code="DISC" vis="Y"><oriVal></oriVal><curVal></curVal></fld>
<fld code="DISC1" vis="Y"><oriVal></oriVal><curVal></curVal></fld>
<fld code="DISC2" vis="Y"><oriVal></oriVal><curVal></curVal></fld>
<fld code="DNR" vis="Y"><oriVal></oriVal><curVal>N</curVal></fld>
<fld code="ER" vis="Y"><oriVal></oriVal><curVal>1</curVal></fld>
</questions>' from dual
)
select x.code, x.curval
from questionnaire q
cross join xmltable('/questions/fld'
  passing xmltype(q.questionxml)
  columns code varchar2(10) path '@code',
    curval varchar2(10) path '/curVal'
) x
where x.curval is not null;

CODE       CURVAL   
---------- ----------
ADV        N         
ASEN1      N         
COGN       A         
COMORBID   1         
DIET       R         
DNR        N         
ER         1         

Read more about using XQuery with XML data