2017-07-07 22 views
0

を使用してCLOBフィールドから抽出xmlタグの値は、私は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> 

私はセクションでは、のSectionIdを持っていることからREQUESTNAMEFieldIdを持つFormFieldタグのFieldValueの値を取得したいですSection_two。結果はJASMINEである必要があります。

私はとしてOracleでクエリを実行しています:

SELECT EXTRACTVALUE(xmltype(req_details), 
    '/FM/SectionsList/Section[@SectionId="Section_two"]/Fields/FormField/FieldValue[@FieldId="REQUESTNAME"]') 
from table 

しかし、結果はNULLです。どうすればOracleの価値を引き出すことができますか?

答えて

0

属性とノードの選択が混乱しています。 SectionIdは、あなたの[@SectionId=...]が探しているセクションの属性ではありません。

あなたはノードのテキスト値を識別し、ツリーをバックアップ歩いてこれを行うことができます:

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 

以上と:

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 

extractvalue()などを代わりのXMLQueryで、推奨されていませんツリーを歩く必要がない明示的なXPath(従うのが少し簡単で、紛失しにくい):

select xmlquery(
    'for $i in /FM/SectionsList/Section where $i/SectionId="Section_two" 
    return 
    for $j in $i/Fields/FormField where $j/FieldId="REQUESTNAME" 
     return $j/FieldValue/text()' 
    passing xmltype(req_details) 
    returning content) as result 
from your_table; 

RESULT    
-------------------- 
JASMINE 
+0

パーフェクト...ありがとうございます:) – user2114865