使用oracle从clob字段中提取xml标记值

时间:2017-07-07 07:16:42

标签: xml oracle

我在DB字段中存储了以下XML数据:

<FM>
<SectionsList>
<Section>
<SectionId>Section_one</SectionId>
</Section>
<Section>
<SectionId>Section_two</SectionId> 
<Fields>
<FormField>
<FieldId>REQUESTID</FieldId>
<FieldValue>ABC1234</FieldValue>
</FormField>
<FormField>
<FieldId>REQUESTNAME</FieldId>
<FieldValue>JASMINE</FieldValue>
</FormField>
</Fields>
</Section>
</SectionsList>
</FM>  

我想从FieldValue FormField的部分中检索FieldId REQUESTNAME SectionId Section_twoJASMINE的值。结果应为SELECT EXTRACTVALUE(xmltype(req_details), '/FM/SectionsList/Section[@SectionId="Section_two"]/Fields/FormField/FieldValue[@FieldId="REQUESTNAME"]') from table

我在Oracle中执行查询:

template: "# if(data.Code != '')" +
          "{# #:data.Name# (#:data.Code#) #}" +
          "else {# #:data.Name# #} #"

但结果是NULL。我如何在Oracle中提取值?

1 个答案:

答案 0 :(得分:0)

您对属性和节点选择感到困惑。 $array = [ "Japan" => "Tokyo Shibuya Office", "Taiwan" => "Taipei Shilin Office", "Korea" => "Seoul Yongsan Office", "Singapore" => "Singapore Novena Office", "Australia" => "Sydney Darlinghurst Office" ]; $define_array = serialize($array); define( "OUROFFICE", $define_array ); $our_office = unserialize(OUROFFICE); print_r($our_office); 不是该部分的属性,这是您SectionId正在寻找的内容。

您可以通过识别节点文本值并向后走树来完成此操作:

[@SectionId=...]
不推荐使用

select extractvalue(xmltype(req_details), '/FM/SectionsList/Section/SectionId[text()="Section_two"]/../Fields/FormField/FieldId[text()="REQUESTNAME"]/../FieldValue') as result from your_table RESULT -------------------- JASMINE ,而是使用XMLQuery:

extractvalue()

或者使用更明确的XPath,避免不得不走回树上(因此更容易理解,更难以迷失):

select xmlquery(
  '/FM/SectionsList/Section/SectionId[text()="Section_two"]/../Fields/FormField/FieldId[text()="REQUESTNAME"]/../FieldValue/text()'
  passing xmltype(req_details)
  returning content) as result
from your_table


RESULT              
--------------------
JASMINE