在oraclesql中从xml节点提取名称和值

vdzxcuhz  于 2021-08-09  发布在  Java
关注(0)|答案(1)|浏览(548)

我试图通过oraclesql查询从soapweb服务响应返回的xml中提取基于名称的值,但是这会导致错误。我可以提取 AdName , AdCat 使用这个 XMLTYPE 方法,但不是那些更深入的方法 Results 以及 AdSettings 属性。我一定是用错语法了。感谢您的帮助。
带有xml响应的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: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;

错误:

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
esyap4oy

esyap4oy1#

最后四列xpath表达式完全关闭。下面是如何正确地做这件事。
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;

相关问题