ORACLE 12c XML:使用EXTRACTVALUE从XML字符串中提取值

时间:2015-11-04 09:42:57

标签: xml oracle

我非常感谢您对此的帮助..

我有一个XML字符串包含在XMLType中,我试图从中提取单个值。我已经花了很多时间基于大量的在线研究,但没有成功......

l_resp_xml(xmltype)中保存的字符串是:

<ns1:validateAccountResponse 
xmlns:ns1="https://ws.hyphen.co.za/accountvalidation2">
<return xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"     xsi:type="tns:WebService_AccountValidation_Response">
     <reference xsi:type="xsd:string">TLPM165U2876100975</reference>
     <branchCode xsi:type="xsd:string">187646</branchCode>
     <bankName xsi:type="xsd:string">NED:NEDBANK LTD RSA</bankName>
     <branchName xsi:type="xsd:string">NEDBANK BRITS</branchName>
     <accountNumber xsi:type="xsd:string">00000002876100975</accountNumber>
     <accountType xsi:type="xsd:string">2</accountType>
     <errorCode xsi:type="xsd:string">0</errorCode>
     <resultCode xsi:type="xsd:string">0000</resultCode>
     <resultMessage xsi:type="xsd:string"/>
     <checkSum xsi:type="xsd:string">db8d375002ca1e62ab4e8696c480df12d7e10293a29ba1a0dc99868d18    aa2c7e</checkSum>
</return></ns1:validateAccountResponse>

我尝试了一些不同的方法来获取bankName的值。最新的只是:

         SELECT 
         EXTRACTVALUE(l_resp_xml, '/return/bankName')
         into l_bank_name
     FROM DUAL; 

我尝试了很多变化,但是无法从bankName获取值(l_bank_name的内容总是空白的......如果有人可以提供帮助,我将不胜感激

非常感谢 理查德

2 个答案:

答案 0 :(得分:1)

EXTRACTVALUEdeprecated for some time。你应该使用XQuery,在这种情况下使用XMLQuery的一个简单路径;另外,您需要引用命名空间ns1,而忽略顶级节点validateAccountResponse

with t (l_resp_xml) as (
  select xmltype('<ns1:validateAccountResponse 
xmlns:ns1="https://ws.hyphen.co.za/accountvalidation2">
<return xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"     xsi:type="tns:WebService_AccountValidation_Response">
     <reference xsi:type="xsd:string">TLPM165U2876100975</reference>
     <branchCode xsi:type="xsd:string">187646</branchCode>
     <bankName xsi:type="xsd:string">NED:NEDBANK LTD RSA</bankName>
     <branchName xsi:type="xsd:string">NEDBANK BRITS</branchName>
     <accountNumber xsi:type="xsd:string">00000002876100975</accountNumber>
     <accountType xsi:type="xsd:string">2</accountType>
     <errorCode xsi:type="xsd:string">0</errorCode>
     <resultCode xsi:type="xsd:string">0000</resultCode>
     <resultMessage xsi:type="xsd:string"/>
     <checkSum xsi:type="xsd:string">db8d375002ca1e62ab4e8696c480df12d7e10293a29ba1a0dc99868d18    aa2c7e</checkSum>
</return></ns1:validateAccountResponse>') from dual
)
SELECT XMLQuery('declare namespace ns1 = "https://ws.hyphen.co.za/accountvalidation2"; (::)
    /ns1:validateAccountResponse/return/bankName/text()'
  PASSING l_resp_xml RETURNING CONTENT) AS bankName
FROM t;

BANKNAME                                                                       
--------------------------------------------------------------------------------
NED:NEDBANK LTD RSA

或者使用您的PL / SQL值:

SELECT CAST(XMLQuery('declare namespace ns1 = "https://ws.hyphen.co.za/accountvalidation2"; (::)
    /ns1:validateAccountResponse/return/bankName/text()'
  PASSING l_resp_xml RETURNING CONTENT) AS VARCHAR2(30))
INTO l_bank_name
FROM dual;

您还可以使用XMLTable更轻松地提取多个值,尤其是当您从表中的行中获取多个XMLType值时:

with t (l_resp_xml) as (
  select xmltype('<ns1:validateAccountResponse 
xmlns:ns1="https://ws.hyphen.co.za/accountvalidation2">
<return xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"     xsi:type="tns:WebService_AccountValidation_Response">
     <reference xsi:type="xsd:string">TLPM165U2876100975</reference>
     <branchCode xsi:type="xsd:string">187646</branchCode>
     <bankName xsi:type="xsd:string">NED:NEDBANK LTD RSA</bankName>
     <branchName xsi:type="xsd:string">NEDBANK BRITS</branchName>
     <accountNumber xsi:type="xsd:string">00000002876100975</accountNumber>
     <accountType xsi:type="xsd:string">2</accountType>
     <errorCode xsi:type="xsd:string">0</errorCode>
     <resultCode xsi:type="xsd:string">0000</resultCode>
     <resultMessage xsi:type="xsd:string"/>
     <checkSum xsi:type="xsd:string">db8d375002ca1e62ab4e8696c480df12d7e10293a29ba1a0dc99868d18    aa2c7e</checkSum>
</return></ns1:validateAccountResponse>') from dual
)
SELECT x.reference, x.bankname, x.branchCode
FROM t
CROSS JOIN XMLTABLE(
  XMLNAMESPACES('https://ws.hyphen.co.za/accountvalidation2' as "ns1"),
  '/ns1:validateAccountResponse/return'
  PASSING t.l_resp_xml
  COLUMNS reference VARCHAR2(20) PATH 'reference',
    bankName VARCHAR2(30) PATH 'bankName',
    branchCode NUMBER PATH 'branchCode'
) x;

REFERENCE            BANKNAME                       BRANCHCODE
-------------------- ------------------------------ ----------
TLPM165U2876100975   NED:NEDBANK LTD RSA                187646

答案 1 :(得分:1)

虽然{@ 1}}已弃用,但这是EXTRACTVALUE的版本:

EXTRACTVALUE

SELECT EXTRACTVALUE(l_resp_xml, '/ns1:validateAccountResponse/return/bankName', 'xmlns:ns1="https://ws.hyphen.co.za/accountvalidation2"') INTO l_bank_name FROM dual; XMLTable是首选方式。