Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
265 views
in Technique[技术] by (71.8m points)

ORACLE 12c XML: Extract value from XML string using EXTRACTVALUE

Id appreciate your help on this one..

I have an XML string contained in an XMLType and Im trying to extract a single value from it.. I've spent ages now based on lots of online research, but without success...

the string held in l_resp_xml (xmltype) is:

<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>

And Ive tried a bunch of different ways to get the value of bankName out. The latest is simply:

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

I've tried lots of variations, but cannot get the value from bankName (the contents of l_bank_name is always blank.. if someone could help, Id be grateful

Many Thanks Richard

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

EXTRACTVALUE has been deprecated for some time. You should be using XQuery, in this case with a single simple path using XMLQuery; plus you need to refer to the namespace ns1, and you are ignoring the top-level node 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

Or with your PL/SQL value:

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;

You could also use an XMLTable to extract multiple values more easily, particularly when you're getting multiple XMLType values from rows in a table:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...