从xmlType提取值

时间:2019-10-10 21:02:10

标签: sql xml oracle oracle11g xml-parsing

我知道之前已经有人问过这个问题,但是我无法格式化自己的猴子

WITH TEST_XML_EXTRACT AS 
( 
    SELECT XMLTYPE (
                    '<tns:Envelope xmlns:tns="http://schemas.xmlsoap.org/soap/envelope/">
                        <testLeaf> ValueIWant </testLeaf>
                     </tns:Envelope>') testField
      FROM dual
) 
SELECT EXTRACTVALUE(testField,'/testLeaf'), -- doesn't work
       EXTRACTVALUE(testField,'/tns'),      -- doesn't work      
       EXTRACTVALUE(testField,'/Envelope'), -- doesn't work
       EXTRACTVALUE(testField,'/BIPIBITY')  -- doesn't work
  FROM TEST_XML_EXTRACT;

它只是返回空白。

我在Oracle文档中找不到任何完全相似的示例。

有什么想法吗?

2 个答案:

答案 0 :(得分:1)

在XPath表达式中只有testLeaf个节点是一个体面定义的节点。因此,只能使用extractValue()函数以以下方式提取这些内容:

with test_xml_extract( testField ) as
(
    select
        XMLType(
        '<tns:Envelope xmlns:tns="http://schemas.xmlsoap.org/soap/envelope/">
            <testLeaf> ValueIWant </testLeaf>
        </tns:Envelope>'
        ) 
      from dual
)
select extractValue(value(t), 'testLeaf') as testLeaf,
       extractValue(value(t), 'tns') as tns,
       extractValue(value(t), 'Envelope') as Envelope,
       extractValue(value(t), 'BIPIBITY') as BIPIBITY
  from test_xml_extract t,
       table(XMLSequence(t.testField.extract('//testLeaf'))) t;

TESTLEAF    TNS      ENVELOPE    BIPIBITY
----------  -------  ----------  ----------
ValueIWant 

Demo

答案 1 :(得分:1)

您最好将名称空间传递给提取过程

WITH TEST_XML_EXTRACT AS    
     ( SELECT XMLTYPE (
             '<tns:Envelope xmlns:tns="http://schemas.xmlsoap.org/soap/envelope/">
              <testLeaf> ValueIWant </testLeaf>
              </tns:Envelope>') testField
      FROM dual)  
 select t.testField.extract('/tns:Envelope/testLeaf', 
             'xmlns:tns="http://schemas.xmlsoap.org/soap/envelope/"').getstringval() val,
       EXTRACTVALUE(t.testField,'/tns:Envelope/testLeaf', 'xmlns:tns="http://schemas.xmlsoap.org/soap/envelope/"') extval,
       EXTRACTVALUE(t.testField,'/*/testLeaf', 'xmlns:tns="http://schemas.xmlsoap.org/soap/envelope/"') extval_wc 
from  TEST_XML_EXTRACT t;