简体   繁体   English

在 Oracle 中,如何提取 XML 列中 XML 项的值?

[英]In Oracle, how can I extract value of XML item in XML column?

I have XML column in Oracle table where i have this string:我在 Oracle 表中有 XML 列,其中我有这个字符串:

<field name="Text1" type="String"><value/> <field name="Text1" type="String"><value/>

I need to extract the value of this field if exists, in the above example it doesn't exists.如果存在,我需要提取该字段的值,在上面的示例中它不存在。 In the below example the value exists:在下面的示例中,该值存在:

<field name="Text1" type="String"><value>12345</value> <field name="Text1" type="String"><value>12345</value>

What will be the best way to do this?最好的方法是什么?

Thank you.谢谢你。

As you said: extract value .正如你所说:提取价值

SQL> with test (col) as
  2    (select '<field name="Text1" type="String"><value>12345</value></field>'
  3     from dual
  4    )
  5  SELECT extractvalue(xmltype(col), '/field/value') result
  6  from test;

RESULT
----------------------------------------------------------------------------------------------------
12345

SQL>

XMLTABLE would be a good choice to use in such a way that XMLTABLE将是一个不错的选择,以这样的方式使用

SELECT x.*
  FROM xml_tab t,
       XMLTABLE('//Root'
                   PASSING xml_data
                    COLUMNS
                        field1      INT PATH 'field[@name="Text1"]/value',
                        field2      INT PATH 'field[@name="Text2"]/value'
                        ) x  

returning返回

FIELD1现场1 FIELD2 FIELD2
12345 12345

where xml_data might be xml_data可能在哪里

<Root>
  <field name="Text1" type="String">
    <value>12345</value>
  </field>
  <field name="Text2" type="String">
    <value/>
  </field>
</Root>

Demo 演示

PS. PS。 The EXTRACTVALUE function is deprecated. EXTRACTVALUE function 已弃用。 It is still supported for backward compatibility.它仍然支持向后兼容。 However, Oracle recommends that you use the XMLTABLE function, or the XMLCAST and XMLQUERY functions instead但是,Oracle 建议您改用XMLTABLE function 或XMLCASTXMLQUERY函数

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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