在xml(Oracle)中使用命名空间时如何通过xmltable解析xml [英] How to parse xml by xmltable when using namespace in xml(Oracle)

查看:65
本文介绍了在xml(Oracle)中使用命名空间时如何通过xmltable解析xml的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想解析一个xml字符串,它是从servier发送的Web服务响应,该xml看起来像这样:

I want to parse a xml string that is a web service response sent from servier, the xml looks like this:

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <soap:Body>
        <addResponse xmlns="http://tempuri.org/">
            <addResult>20</addResult>
        </addResponse>
    </soap:Body>
</soap:Envelope>

我想获取元素addResult之间的值20.我的plsql代码段如下所示:

I want to get the value 20 between elements addResult. My plsql code segment looks like following:

declare
  v_xml clob;
begin
  v_xml := '<?xml version="1.0" encoding="utf-8"?>
  <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <soap:Body>
      <addResponse xmlns="http://tempuri.org/">
        <addResult>20</addResult>
      </addResponse>
    </soap:Body>
  </soap:Envelope>';
  for c in (select results 
        from xmltable('Envelope/Body/addResponse' passing xmltype(v_xml) 
        columns results varchar(100) path './addResult')
       )
  loop
    dbms_output.put_line('the result of calculation is : ' || c.results);
  end loop;
end;

似乎什么也没打印出来,但是如果我删除命名空间"soap",代码运行良好,那么有人可以告诉我当xml具有命名空间时我如何获得值20?

seems that nothing was printed out, but if I remove the namespace 'soap', the code works well, so can anybody tell me how can I got the value 20 when the xml has namespace?

推荐答案

基于此答案

应该是这样的:

declare    
  v_xml clob;    
begin    
  v_xml := '<?xml version="1.0" encoding="utf-8"?>    
  <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">    
    <soap:Body>    
      <addResponse xmlns="http://tempuri.org/">    
        <addResult>20</addResult>    
      </addResponse>    
    </soap:Body>    
  </soap:Envelope>';    
  for c in (select results    
              from xmltable(xmlnamespaces(default 'http://tempuri.org/',    
                                          'http://schemas.xmlsoap.org/soap/envelope/' as    
                                          "soap" ),    
                            'soap:Envelope/soap:Body/addResponse' passing    
                            xmltype(v_xml) columns results varchar(100) path    
                            './addResult')) loop    
    dbms_output.put_line('the result of calculation is : ' || c.results);    
  end loop;    
end;

这篇关于在xml(Oracle)中使用命名空间时如何通过xmltable解析xml的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆