Извлечь значения из xmlType - PullRequest
1 голос
/ 11 октября 2019

Я знаю, что об этом уже спрашивали, но я не могу сделать обезьянку-увидеть-обезьяну-сделать с моим форматированием

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 ]

1 голос
/ 11 октября 2019

Вы могли бы лучше передать пространство имен процессу извлечения

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;
1 голос
/ 11 октября 2019

В выражении 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

...