Xml 使用PL/SQL解析SOAP响应

Xml 使用PL/SQL解析SOAP响应,xml,oracle,soap,plsql,Xml,Oracle,Soap,Plsql,我试图使用以下代码解析SOAP响应,但得到的是空响应。响应包含多个名称空间,我认为这就是问题所在。有人能指出我做错了什么吗 declare l_xml xmltype; begin l_xml := xmltype.createXML('<?xml version="1.0" encoding="UTF-8"?> <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/

我试图使用以下代码解析SOAP响应,但得到的是空响应。响应包含多个名称空间,我认为这就是问题所在。有人能指出我做错了什么吗

declare
  l_xml  xmltype;
begin
  l_xml := xmltype.createXML('<?xml version="1.0" encoding="UTF-8"?>
                <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://www.cachelan.com/WebAPI/solarvuDataRetrieve.php" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:ns2="http://xml.apache.org/xml-soap" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
                <SOAP-ENV:Body>
                    <ns1:getDataResponse>
                        <return xsi:type="ns2:Map">
                            <item>
                                <key xsi:type="xsd:string">status</key>
                                <value xsi:type="xsd:int">0</value>
                            </item>
                            <item>
                                <key xsi:type="xsd:string">content</key>
                                <value xsi:type="xsd:string">"SolarVu OM Data Request"
                                "From Mar 1, 2017 To Mar 4, 2017"

                                "Timestamp","Date","Daily Energy(kWh)","Insolation(Wh/m^2)"
                                "1488344400","Mar 1/17","131.064","1,105.750"
                                "1488430800","Mar 2/17","370.576","3,202.750"
                                "1488517200","Mar 3/17","517.566","4,662.500"
                                "1488603600","Mar 4/17","382.626","6,001.750"
                            </value>
                        </item>
                    </return>
                </ns1:getDataResponse>
                </SOAP-ENV:Body>
                </SOAP-ENV:Envelope>');

  for i in (SELECT t."key", t."value"
            FROM dual d,
            XMLTABLE(xmlnamespaces('http://www.w3.org/2001/XMLSchema-instance' as "xsi",
                                   'http://schemas.xmlsoap.org/soap/envelope/' as "SOAP-ENV",
                                   'http://www.cachelan.com/WebAPI/solarvuDataRetrieve.php' as "ns1",
                                   'http://www.w3.org/2001/XMLSchema' as "xsd",
                                   'http://xml.apache.org/xml-soap' as "ns2",
                                   'http://schemas.xmlsoap.org/soap/encoding/' as "SOAP-ENC"), 
                    '/getDataResponse/return/item[*]'
              PASSING l_xml
              COLUMNS 
                 "key"   varchar2(4000) PATH 'key',
                 "value" varchar2(4000) PATH 'value') t)
  loop
    dbms_output.put_line('Key is: '||i."key");
  end loop;
end;
/

我也有同样的问题。我发现问题出在前缀名称空间中,比如Oracle XMLType以及如何解释它们。根据您的示例,我解决了提取没有前缀名称空间的内部节点的问题

declare
   l_xml xmltype;
begin
   l_xml := xmltype.createXML('<?xml version="1.0" encoding="UTF-8"?>
                <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://www.cachelan.com/WebAPI/solarvuDataRetrieve.php" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:ns2="http://xml.apache.org/xml-soap" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
                <SOAP-ENV:Body>
                    <ns1:getDataResponse>
                        <return xsi:type="ns2:Map">
                            <item>
                                <key xsi:type="xsd:string">status</key>
                                <value xsi:type="xsd:int">0</value>
                            </item>
                            <item>
                                <key xsi:type="xsd:string">content</key>
                                <value xsi:type="xsd:string">"SolarVu OM Data Request"
                                "From Mar 1, 2017 To Mar 4, 2017"

                                "Timestamp","Date","Daily Energy(kWh)","Insolation(Wh/m^2)"
                                "1488344400","Mar 1/17","131.064","1,105.750"
                                "1488430800","Mar 2/17","370.576","3,202.750"
                                "1488517200","Mar 3/17","517.566","4,662.500"
                                "1488603600","Mar 4/17","382.626","6,001.750"
                            </value>
                        </item>
                    </return>
                </ns1:getDataResponse>
                </SOAP-ENV:Body>
                </SOAP-ENV:Envelope>');

   --Pay attention here and changes in path parameter at XMLTABLE
   l_xml := l_xml.EXtract('//*/*//return');

   for i in (select t.key, t.value
               from dual d,
                    XMLTABLE('/return/item' PASSING l_xml COLUMNS key
                               varchar2(4000) PATH 'key',
                              value varchar2(4000) PATH 'value') t) loop
      dbms_output.put_line('Key is: ' || i.key);
   end loop;
end;
您没有在XPath中使用getDataResponse的名称空间作为前缀,也没有包括信封和正文标记;因此,将XPath更改为:

'/SOAP-ENV:Envelope/SOAP-ENV:Body/ns1:getDataResponse/return/item'
仅通过这些更改,您的代码就会生成:

Key is: status
Key is: content

PL/SQL procedure successfully completed.

谢谢Alex,我根据你的建议完成了这项工作。谢谢你给我的提示Seyran,我会在下次使用这个技巧。这看起来有点像黑客,如果你需要来自多个返回节点或一个节点内多个层的数据,尤其是如果有任何节点有名称空间的话,这项工作就不会真正起作用。如果这样做,xmlnamespaces子句是多余的,因为提取的节点没有名称空间前缀。@Alex Poole它有指向名称空间类型ex的链接。如果省略xmlnamespace,您将得到一个错误。我在删除xmlnamespaces子句的情况下运行了您的代码,它工作正常。名称空间位于本例中未被引用的属性上,因此我猜它未被选中?抱歉,同意@Alex Poole。