2016-06-28 80 views
1

我嘗試在oracle服務器中名爲HistEvents的表中將名爲ANI_FATHER的字段的值從「DAD01」更改爲「DAD01」。如何更改Oracle服務器中表中xml字段的值

字段ANI_FATHER位於表HistEvents中名爲HEV_DETAILS的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> 

回答

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

+0

嗨的Arkadiusz盧卡西維茨,我會感激你的答覆和教程的鏈接。我遵循你的腳本,它運行完美。再次感謝您的幫助!埃德。 JD – EdwardJd

相關問題