如何在oracle中的xmltype列中添加和删除现有数据的xml标记

时间:2016-02-01 13:33:22

标签: xml oracle

我创建了下表: enter image description here

这是1个数据行样本:

enter image description here

在此表的第SALPROFILE列中,每个列数据都包含一个大的xml数据,如下例所示:

<employee>
  <id>FMCSC00015</id>
  <year>2016</year>
  <month>1</month>
  <PAYMSTR_SALHDNM>BASIC PAY</PAYMSTR_SALHDNM>
  <PAYMSTR_AMOUNT>35600</PAYMSTR_AMOUNT>
  <PAYMSTR_SALHDNM>ASSOCIATION SUBSCRIPTION</PAYMSTR_SALHDNM>
  <PAYMSTR_AMOUNT>240</PAYMSTR_AMOUNT>
  <PAYMSTR_SALHDNM>TELEPHONE ALLOWANCE</PAYMSTR_SALHDNM>
  <PAYMSTR_AMOUNT>800</PAYMSTR_AMOUNT>
  <PAYMSTR_SALHDNM>HOUSE RENT DEDUCTION</PAYMSTR_SALHDNM>
  <PAYMSTR_AMOUNT>2587.5</PAYMSTR_AMOUNT>
  <PAYMSTR_SALHDNM>MEDICAL ALLOWANCE</PAYMSTR_SALHDNM>
  <PAYMSTR_AMOUNT>700</PAYMSTR_AMOUNT>
  <PAYMSTR_SALHDNM>GAS BILL</PAYMSTR_SALHDNM>
  <PAYMSTR_AMOUNT>450</PAYMSTR_AMOUNT>  
  <PAYMSTR_SALHDNM>LIFE INSURANCE PREMIUM (D)</PAYMSTR_SALHDNM>
  <PAYMSTR_AMOUNT>1718</PAYMSTR_AMOUNT>  
</employee>

我想从上面的示例添加和删除xml标签。 例如,我想删除

<PAYMSTR_SALHDNM>BASIC PAY</PAYMSTR_SALHDNM>
      <PAYMSTR_AMOUNT>35600</PAYMSTR_AMOUNT>

我要添加

 <PAYMSTR_SALHDNM>BANK LOAN</PAYMSTR_SALHDNM>
  <PAYMSTR_AMOUNT>23490</PAYMSTR_AMOUNT>

我该怎么做呢。这可能做到这一点而不追踪整个xml数据以使其更快。我看到Appendchildxml function已被弃用

1 个答案:

答案 0 :(得分:1)

您可以通过AppendChildXMLdeleteXML添加/删除类似以下内容的标记来修改XML。

在该示例中,第一部分返回XML,其中一个标记更多,而第二列删除与[...]之间条件匹配的标记:

with xmlTab(val) as (
    select xmlType('<employee>
      <id>FMCSC00015</id>
      <year>2016</year>
      <month>1</month>
      <PAYMSTR_SALHDNM>BASIC PAY</PAYMSTR_SALHDNM>
      <PAYMSTR_AMOUNT>35600</PAYMSTR_AMOUNT>
      <PAYMSTR_SALHDNM>ASSOCIATION SUBSCRIPTION</PAYMSTR_SALHDNM>
      <PAYMSTR_AMOUNT>240</PAYMSTR_AMOUNT>
      <PAYMSTR_SALHDNM>TELEPHONE ALLOWANCE</PAYMSTR_SALHDNM>
      <PAYMSTR_AMOUNT>800</PAYMSTR_AMOUNT>
      <PAYMSTR_SALHDNM>HOUSE RENT DEDUCTION</PAYMSTR_SALHDNM>
      <PAYMSTR_AMOUNT>2587.5</PAYMSTR_AMOUNT>
      <PAYMSTR_SALHDNM>MEDICAL ALLOWANCE</PAYMSTR_SALHDNM>
      <PAYMSTR_AMOUNT>700</PAYMSTR_AMOUNT>
      <PAYMSTR_SALHDNM>GAS BILL</PAYMSTR_SALHDNM>
      <PAYMSTR_AMOUNT>450</PAYMSTR_AMOUNT>  
      <PAYMSTR_SALHDNM>LIFE INSURANCE PREMIUM (D)</PAYMSTR_SALHDNM>
      <PAYMSTR_AMOUNT>1718</PAYMSTR_AMOUNT>  
    </employee>')
    from dual
    )
    select APPENDCHILDXML( val,
                           'employee',
                           XMLType('<PAYMSTR_SALHDNM>SONALI BANK LOAN-4</PAYMSTR_SALHDNM>')
                         ) as APPEND,  
           DELETEXML(val,'/employee/PAYMSTR_AMOUNT[.="1718"]') AS DEL
    from xmlTab

要添加的XML可以通过从另一个表中读取数据来构建,例如以这种方式:

with test as 
(
    select 'PAYMSTR_SALHDNM' tag, 'SONALI BANK LOAN-4' val from dual union all
    select 'PAYMSTR_SALHDNM' tag, 'GAS BILL' val from dual
)     
select XMLELEMENT(tag, val) from test