2017-07-07 47 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