【问题标题】:Extract values from xmlType从 xmlType 中提取值
【发布时间】:2020-02-08 09:06:03
【问题描述】:

我知道以前有人问过这个问题,但我无法使用我的格式进行猴看猴做

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

有什么想法吗?

【问题讨论】:

    标签: sql xml oracle oracle11g xml-parsing


    【解决方案1】:

    只有testLeaf 节点作为XPath 表达式中定义得当的节点。因此,只能通过以下方式使用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

    【讨论】:

      【解决方案2】:

      将命名空间传递给提取进程可能会更好

      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;
      

      【讨论】:

        猜你喜欢
        • 2013-07-06
        • 1970-01-01
        • 2019-08-19
        • 1970-01-01
        • 1970-01-01
        • 2018-10-06
        • 2022-10-06
        • 2021-05-13
        • 1970-01-01
        相关资源
        最近更新 更多