简体   繁体   中英

Extract values from xmlType

I know this has been asked before, but I'm not able to monkey-see-monkey-do with my formatting

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;

It just returns blank.

And I can't find any exactly similar examples from the Oracle docs.

Any thoughts?

There's only testLeaf node as a decently defined one in the XPath expression. So, only that could be extracted by using extractValue() function in such a way below:

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

You might do better passing the namespace to the extract process

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM