更新 XML Clob 节点

问题描述

我有一个表 Transaction ,其中我有一个 clob xclob ,

我想将“属性”节点的“record_start_dll_date”值更新为 record_start_date(即我想删除 _dll 部分) 和“record_dll_end_date”到 record_end_date 。 我正在使用 oracle 数据库。如何修改这些节点值??

<?xml version ="1.0"?>
<properties>
   <property name ="record_start_dll_date">
   <value>1/1/2021</value>
   </property>
    <property name ="record_dll_end_date">
   <value>21/12/2021</value>
   </property>
</properties>

解决方法

您可以使用带有特定属性值的 XMLQuery 更新:

xmlquery ('copy $i := $p1 modify (
  (for $j in $i/properties/property[@name="record_start_dll_date"]/@name
    return replace value of node $j with $p2),(for $j in $i/properties/property[@name="record_dll_end_date"]/@name
    return replace value of node $j with $p3)
  ) return $i'
  passing xmltype(xclob) as "p1",'record_start_date' as "p2",'record_end_date' as "p3"
  returning content)

或去除任何 _dll

xmlquery ('copy $i := $p1 modify (
  for $j in $i/properties/property[contains(@name,"_dll")]/@name
    return replace value of node $j with replace($j,"_dll","")
  ) return $i'
  passing xmltype(xclob) as "p1"
  returning content)

无论哪种方式,您都可以将其合并到更新语句中,并使用匹配的 XMLExists 子句以避免不必要的更新:

update transaction
set xclob = xmlquery ('copy $i := $p1 modify (
  for $j in $i/properties/property[contains(@name,"")
  ) return $i'
  passing xmltype(xclob) as "p1"
  returning content).getclobval()
where xmlexists('$p1/properties/property[contains(@name,"_dll")]'
  passing xmltype(xclob) as "p1")

db<>fiddle