2017-07-07 43 views
0

我已經存儲在數據庫字段中輸入以下XML數據:提取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具有的REQUESTNAMEFieldIdFormField標籤從部分具有SectionIdSection_two。結果應該是JASMINE

我在甲骨文執行查詢爲:

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

但結果爲NULL。我如何提取Oracle中的值?

回答

0

您在混淆屬性和節點選擇。 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 

extractvalue()已過時,有一個XMLQUERY來代替:

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 

或以更高顯式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