在ORACLE 10G中将值更新为XML

时间:2017-06-20 16:15:55

标签: oracle

我在文件夹中有XML,我想找到并替换一些值。怎么做?

示例:无论UK0在哪里,我都想使用UPDATE QUERY替换UK1。

<?xml version="1.0"?>
<RSET>
 <ROW>
  <SDC_FNAME>JQ13868001.XML</SDC_FNAME>
  <SDC_RECORD>6</SDC_RECORD>
  <SDC_FROMDT>06/14/2017 08:13:58</SDC_FROMDT>
  <SDC_TODT>06/16/2017 08:13:58</SDC_TODT>
  <SDC_TNAME>S_STYLE</SDC_TNAME>
  <SDC_FROM>AB</SDC_FROM>
  <SDC_TO>UK0</SDC_TO>
 </ROW>
 <ROW>
  <SDC_FNAME>JQ13868002.XML</SDC_FNAME>
  <SDC_RECORD>6</SDC_RECORD>
  <SDC_FROMDT>06/14/2017 08:13:58</SDC_FROMDT>
  <SDC_TODT>06/16/2017 08:13:58</SDC_TODT>
  <SDC_TNAME>S_STYLE</SDC_TNAME>
  <SDC_FROM>AB</SDC_FROM>
  <SDC_TO>UK0</SDC_TO>
 </ROW>
</RSET>

1 个答案:

答案 0 :(得分:0)

在10g中,您可以使用the updatexml() function

select updatexml(your_xml, '/RSET/ROW/SDC_TO[text()="UK0"]/text()', 'UK1')
from ...

演示(在11gR2中运行以使用indent的{​​{1}}子句,只是为了让它更漂亮; xmlserialize()部分在10gR2中运行):

updatexml()

该函数在以后的版本中已被弃用,因此(假设这在10g中有效,我现在无法验证)从11g开始,你应该使用XQuery:

select xmlserialize(content
  updatexml(xmltype('<?xml version="1.0"?>
<RSET>
 <ROW>
  <SDC_FNAME>JQ13868001.XML</SDC_FNAME>
  <SDC_RECORD>6</SDC_RECORD>
  <SDC_FROMDT>06/14/2017 08:13:58</SDC_FROMDT>
  <SDC_TODT>06/16/2017 08:13:58</SDC_TODT>
  <SDC_TNAME>S_STYLE</SDC_TNAME>
  <SDC_FROM>AB</SDC_FROM>
  <SDC_TO>UK0</SDC_TO>
 </ROW>
 <ROW>
  <SDC_FNAME>JQ13868002.XML</SDC_FNAME>
  <SDC_RECORD>6</SDC_RECORD>
  <SDC_FROMDT>06/14/2017 08:13:58</SDC_FROMDT>
  <SDC_TODT>06/16/2017 08:13:58</SDC_TODT>
  <SDC_TNAME>S_STYLE</SDC_TNAME>
  <SDC_FROM>AB</SDC_FROM>
  <SDC_TO>UK1</SDC_TO>
 </ROW>
</RSET>'), '/RSET/ROW/SDC_TO[text()="UK0"]/text()', 'UK1')
    indent) as result
from dual;

RESULT                                                                          
--------------------------------------------------------------------------------
<?xml version="1.0"?>
<RSET>
  <ROW>
    <SDC_FNAME>JQ13868001.XML</SDC_FNAME>
    <SDC_RECORD>6</SDC_RECORD>
    <SDC_FROMDT>06/14/2017 08:13:58</SDC_FROMDT>
    <SDC_TODT>06/16/2017 08:13:58</SDC_TODT>
    <SDC_TNAME>S_STYLE</SDC_TNAME>
    <SDC_FROM>AB</SDC_FROM>
    <SDC_TO>UK1</SDC_TO>
  </ROW>
  <ROW>
    <SDC_FNAME>JQ13868002.XML</SDC_FNAME>
    <SDC_RECORD>6</SDC_RECORD>
    <SDC_FROMDT>06/14/2017 08:13:58</SDC_FROMDT>
    <SDC_TODT>06/16/2017 08:13:58</SDC_TODT>
    <SDC_TNAME>S_STYLE</SDC_TNAME>
    <SDC_FROM>AB</SDC_FROM>
    <SDC_TO>UK1</SDC_TO>
  </ROW>
</RSET>

但10gR2不支持该语法。

无论如何

演示(在11gR2下运行):

select xmlquery('
  copy $i := $p modify (
    for $j in $i/RSET/ROW/SDC_TO[text()="UK0"]
      return replace value of node $j with "UK1"
    )
    return $i'
  passing your_xml AS "p"
  returning content)
from ...