如何在oracle服务器的表中更改xml字段的值

时间:2016-06-28 20:55:31

标签: xml oracle

我尝试从" DAD01"更改名为ANI_FATHER的字段的值到" DAD01"在oracle服务器中名为HistEvents的表中。

字段ANI_FATHER位于表HEV_DETAILS中名为HistEvents的XML字段中,我不知道如何更改其值。

screenshot of xml field when I do select in an oracle table

<?xml version = '1.0' encoding = 'UTF-8'?><BIRTH Id="1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
   <ANI_BIRTHDATE xsi:type="xsd:datetime">2015-12-31T00:00:00</ANI_BIRTHDATE>
   <ANI_MOTHER>MOM01</ANI_MOTHER>
   <ANI_FATHER>DAD01</ANI_FATHER>
   <ANI_NUMGEN>F1</ANI_NUMGEN>
   <ANI_NAIVE xsi:type="xsd:boolean">1</ANI_NAIVE>
   <ANI_DATEIN xsi:type="xsd:datetime">2015-12-31T00:00:00</ANI_DATEIN>
</BIRTH>

1 个答案:

答案 0 :(得分:0)

create table test_xml(id number, xml_col xmltype);

insert into test_xml values(1,xmltype(q'~<?xml version = '1.0' encoding = 'UTF-8'?><BIRTH Id="1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
   <ANI_BIRTHDATE xsi:type="xsd:datetime">2015-12-31T00:00:00</ANI_BIRTHDATE>
   <ANI_MOTHER>MOM01</ANI_MOTHER>
   <ANI_FATHER>DAD01</ANI_FATHER>
   <ANI_NUMGEN>F1</ANI_NUMGEN>
   <ANI_NAIVE xsi:type="xsd:boolean">1</ANI_NAIVE>
   <ANI_DATEIN xsi:type="xsd:datetime">2015-12-31T00:00:00</ANI_DATEIN>
</BIRTH>~'));

1)第一种方式。看起来更容易,但不推荐使用updateXml函数。

UPDATE test_xml SET xml_col = 
  updateXML(xml_col,  
            '/BIRTH/ANI_FATHER/text()',
            'neW_VALUE')
            WHERE ID =1 ;  

并检查结果

 SELECT XMLSERIALIZE(DOCUMENT XML_COL) FROM TEST_XML WHERE ID= 1;

1)推荐方式。

UPDATE test_xml SET xml_col =
  XMLQuery('copy $tmp := . modify
            (for $i in $tmp/BIRTH/ANI_FATHER/text()  
             return replace value of node $i
                    with ''neW_VALUE2'') 
            return $tmp'
           PASSING xml_col RETURNING CONTENT)
  WHERE ID =1;

XML Updating Queries to XQuery Update

Xpath tutorial