簡體   English   中英

從 xmlType 中提取值

[英]Extract values from xmlType

我知道以前有人問過這個問題,但是我無法用我的格式來做猴子看猴子

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 文檔中找不到任何完全相同的示例。

有什么想法嗎?

XPath 表達式中只有testLeaf節點作為一個定義得體的節點。 因此,只能通過以下方式使用extractValue() function 提取:

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 

演示

您可能會更好地將名稱空間傳遞給提取過程

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;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM