0

I am trying to extract the Values based on Name via Oracle SQL query from the XML returned by a SOAP web service response, however it's resulting in errors. I could extract AdName, AdCat using this XMLTYPE method, but not the ones that's embedded further inside Results and AdSettings attributes. I must be wrong with the syntax I am using. Any help is appreciated.

SQL Query with XML Response:

WITH xmltest (id, data_xml) AS ( 
SELECT 1, XMLTYPE('<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <soap:Header>
    <ResponseHeader xmlns="https://www.google.com/apis/ads/v202005">
      <requestId>123456</requestId>
    </ResponseHeader>
  </soap:Header>
  <soap:Body>
    <getAdsResponse xmlns="https://www.google.com/apis/ads/v202005">
      <getAdsResult>
        <ResultReceived>1</ResultReceived>
        <AdName>Target Comm. Systems</AdName>
        <AdCat>COMMERCIAL</AdCat>
        <SessionID>654321</SessionID>
        <Results>
          <Result><Name>ADId</Name><Value>2372</Value></Result>
          <Result><Name>AdStatus</Name><Value>ACTIVE</Value></Result>
          <Result><Name>targetWindow</Name><Value>TOP</Value></Result>
        </Results>
        <AdSettings>
          <Setting><Name>fontFamily</Name><Value>DEFAULT</Value></Setting>
          <Setting><Name>fontSize</Name><Value>9</Value></Setting>
          <Setting><Name>adSenseEnabled</Name><Value>TRUE</Value></Setting>
          <Setting><Name>adType</Name><Value>TEXT_AND_IMAGE</Value></Setting>
        </AdSettings>
      </getAdsResult>
    </getAdsResponse>
  </soap:Body>
</soap:Envelope>') FROM SYS.DUAL
  )
  SELECT x.Ad_Name,
         x.Ad_Category,
         x.Ad_ID,
         x.Ad_Status,
         x.AdSense_Enabled,
         x.Ad_Type
  FROM   xmltest d,  
         XMLTABLE ( XMLNAMESPACES ( 'http://schemas.xmlsoap.org/soap/envelope/'  AS  "soap",
                                    'https://www.google.com/apis/ads/v202005'    AS  "AdsNs2" 
                                  ),
                    '/soap:Envelope/soap:Body/AdsNs2:getAdsResponse/AdsNs2:getAdsResult'
                    PASSING d.data_xml
                    COLUMNS Ad_Name          VARCHAR2(50)  PATH  'AdsNs2:AdName',
                            Ad_Category      VARCHAR2(50)  PATH  'AdsNs2:AdCat',
                            Ad_ID            VARCHAR2(50)  PATH  'AdsNs2/Results:Result[Name="ADId"]/Value',
                            Ad_Status        VARCHAR2(50)  PATH  'AdsNs2/Results:Result[Name="AdStatus"]/Value',
                            AdSense_Enabled  VARCHAR2(50)  PATH  'AdsNs2/Results:Result[Name="adSenseEnabled"]/Value',
                            Ad_Type          VARCHAR2(50)  PATH  'AdsNs2/Results:Result[Name="adType"]/Value'
                  ) x;

Error:

ORA-19112: error raised during evaluation: 
XVM-01081: [XPST0081] Invalid prefix
1   declare namespace soap="http://schemas.xmlsoap.org/soap/envelope/";declare 
-                                                                                                                                               ^

19112. 00000 -  "error raised during evaluation: %s"
*Cause:    The error function was called during evaluation of the XQuery expression.
*Action:   Check the detailed error message for the possible causes.
Error at Line: 39 Column: 10

1 Answer 1

2

The last four columns XPath expressions were completely off. Here is how to do it correctly.

SQL

WITH xmltest (id, data_xml) AS ( 
SELECT 1, XMLTYPE('<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <soap:Header>
    <ResponseHeader xmlns="https://www.google.com/apis/ads/v202005">
      <requestId>123456</requestId>
    </ResponseHeader>
  </soap:Header>
  <soap:Body>
    <getAdsResponse xmlns="https://www.google.com/apis/ads/v202005">
      <getAdsResult>
        <ResultReceived>1</ResultReceived>
        <AdName>Target Comm. Systems</AdName>
        <AdCat>COMMERCIAL</AdCat>
        <SessionID>654321</SessionID>
        <Results>
          <Result><Name>ADId</Name><Value>2372</Value></Result>
          <Result><Name>AdStatus</Name><Value>ACTIVE</Value></Result>
          <Result><Name>targetWindow</Name><Value>TOP</Value></Result>
        </Results>
        <AdSettings>
          <Setting><Name>fontFamily</Name><Value>DEFAULT</Value></Setting>
          <Setting><Name>fontSize</Name><Value>9</Value></Setting>
          <Setting><Name>adSenseEnabled</Name><Value>TRUE</Value></Setting>
          <Setting><Name>adType</Name><Value>TEXT_AND_IMAGE</Value></Setting>
        </AdSettings>
      </getAdsResult>
    </getAdsResponse>
  </soap:Body>
</soap:Envelope>') FROM SYS.DUAL
  )
  SELECT x.Ad_Name,
         x.Ad_Category,
         x.Ad_ID,
         x.Ad_Status,
         x.AdSense_Enabled,
         x.Ad_Type
  FROM   xmltest d,  
         XMLTABLE ( XMLNAMESPACES ( 'http://schemas.xmlsoap.org/soap/envelope/'  AS  "soap",
                                    'https://www.google.com/apis/ads/v202005'    AS  "AdsNs2" 
                                  ),
                    '/soap:Envelope/soap:Body/AdsNs2:getAdsResponse/AdsNs2:getAdsResult'
                    PASSING d.data_xml
                    COLUMNS Ad_Name          VARCHAR2(50)  PATH  'AdsNs2:AdName',
                            Ad_Category      VARCHAR2(50)  PATH  'AdsNs2:AdCat',
                            Ad_ID            VARCHAR2(50)  PATH  'AdsNs2:Results/AdsNs2:Result[AdsNs2:Name="ADId"]/AdsNs2:Value',
                            Ad_Status        VARCHAR2(50)  PATH  'AdsNs2:Results/AdsNs2:Result[AdsNs2:Name="AdStatus"]/AdsNs2:Value',
                            AdSense_Enabled  VARCHAR2(50)  PATH  'AdsNs2:AdSettings/AdsNs2:Setting[AdsNs2:Name="adSenseEnabled"]/AdsNs2:Value',
                            Ad_Type          VARCHAR2(50)  PATH  'AdsNs2:AdSettings/AdsNs2:Setting[AdsNs2:Name="adType"]/AdsNs2:Value'
                  ) x;
Sign up to request clarification or add additional context in comments.

2 Comments

Exactly what I was looking for! I knew I messed up the formatting. Thank you so much @yitzhak-khabinsky for a quick response :)
@NiCKz, good to hear that it is working for you. Please connect with me on LinkedIn.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.