xml - 在 xml 中使用命名空间时如何通过 xmltable 解析 xml(Oracle)

标签 xml oracle plsql

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 代码段如下所示:

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;

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?

最佳答案

基于 this answer

应该是这样的:

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 - 在 xml 中使用命名空间时如何通过 xmltable 解析 xml(Oracle),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22219076/

相关文章:

java - Android:OnItemCLickListener 在 ListView 中不工作

python - 使用 Python 抓取特定页面

c - libxml2 中奇怪的 XPath 行为

oracle - 如何使用to_sql将pandas数据帧写入oracle数据库?

java - 如何使用 SQL 调用以数组作为参数的 pl/SQL 函数

regex - Ora 06512/04088 在 INSERT INTO 语句时触发错误

javascript - 如何从js向xml文件添加内容

sql - SQL 查询 : possibly using CASE WHEN 中的复杂排序

c++ - 使用 ' Proc C-C` 获取表中的列数`

oracle - PL/SQL : ORA-00932: inconsistent datatypes: expected UDT got NUMBER