删除具有特定 CDATA 的 xml 个节点
Delete xml node with a specific CDATA
这是我的一部分 XML:
<EO Name="oracle.apps.hcm.goals.core.publicModel.entity.GoalMeasurementEO">
<![CDATA[0001FFFFFFFF]]>
<GoalMeasurementEORow PS="0" Hdl="16" PK="Y" CI="Y" AV="111111111111111111111111111111">
<MeasurementId>
<DATA null="true"/>
</MeasurementId>
</GoalMeasurementEORow>
</EO>
<EO Name="oracle.apps.hcm.goals.core.publicModel.entity.GoalMeasurementEO">
<![CDATA[00010000000EACED00057708000110D943311B2F]]>
<GoalMeasurementEORow PS="0" Hdl="1019" PK="Y" CI="Y" AV="">
<MeasurementId>
<DATA>300000297949999</DATA>
</MeasurementId>
<BusinessGroupId>
<DATA>1</DATA>
</BusinessGroupId>
</EO>
我需要使用 Oracle 在 table 中删除带有 <![CDATA[0001FFFFFFFF]]>
的 EO 节点。此 xml 存储在 XMLTYPE 的 XM_DATA_CACHE 中。
有没有办法根据CDATA
删除
最终输出应该是
<EO Name="oracle.apps.hcm.goals.core.publicModel.entity.GoalMeasurementEO">
<![CDATA[0001FFFFFFFF]]>
<GoalMeasurementEORow PS="0" Hdl="16" PK="Y" CI="Y" AV="111111111111111111111111111111">
<MeasurementId>
<DATA null="true"/>
</MeasurementId>
</GoalMeasurementEORow>
</EO> ```
首先你的 xml 坏了:第二个标签 GoalMeasurementEORow
没有关闭,而且因为它只是 xml 的一部分,它没有根标签。
无论如何,我已经修复了你的 xml 以展示你如何从你的 xml.
中删除 cdata
请看下面的例子:
-- sample data:
with t(x) as (
select xmltype(q'{
<ROOT>
<EO Name="oracle.apps.hcm.goals.core.publicModel.entity.GoalMeasurementEO">
<![CDATA[0001FFFFFFFF]]>
<GoalMeasurementEORow PS="0" Hdl="16" PK="Y" CI="Y" AV="111111111111111111111111111111">
<MeasurementId>
<DATA null="true"/>
</MeasurementId>
</GoalMeasurementEORow>
</EO>
<EO Name="oracle.apps.hcm.goals.core.publicModel.entity.GoalMeasurementEO">
<![CDATA[00010000000EACED00057708000110D943311B2F]]>
<GoalMeasurementEORow PS="0" Hdl="1019" PK="Y" CI="Y" AV="">
<MeasurementId>
<DATA>300000297949999</DATA>
</MeasurementId>
<BusinessGroupId>
<DATA>1</DATA>
</BusinessGroupId>
</GoalMeasurementEORow>
</EO>
</ROOT>
}')
from dual)-- end of sample data
-- main query:
select
XMLQuery(
'copy $i := $p1 modify
(for $j in $i//EO
let $o := <EO >{$j/@*}{ $j/node()[name() ne ""]}</EO>
return replace node $j with $o
)
return $i'
PASSING t.x AS "p1"
returning content
) xdata_new
from t;
结果:
XDATA_NEW
----------------------------------------------------------------------------------
<ROOT>
<EO Name="oracle.apps.hcm.goals.core.publicModel.entity.GoalMeasurementEO">
<GoalMeasurementEORow PS="0" Hdl="16" PK="Y" CI="Y" AV="111111111111111111111111111111">
<MeasurementId>
<DATA null="true"/>
</MeasurementId>
</GoalMeasurementEORow>
</EO>
<EO Name="oracle.apps.hcm.goals.core.publicModel.entity.GoalMeasurementEO">
<GoalMeasurementEORow PS="0" Hdl="1019" PK="Y" CI="Y" AV="">
<MeasurementId>
<DATA>300000297949999</DATA>
</MeasurementId>
<BusinessGroupId>
<DATA>1</DATA>
</BusinessGroupId>
</GoalMeasurementEORow>
</EO>
</ROOT>
如您所见,我正在用它们的内容替换所有子节点 EO
,CDATA 消失。
这是我的一部分 XML:
<EO Name="oracle.apps.hcm.goals.core.publicModel.entity.GoalMeasurementEO">
<![CDATA[0001FFFFFFFF]]>
<GoalMeasurementEORow PS="0" Hdl="16" PK="Y" CI="Y" AV="111111111111111111111111111111">
<MeasurementId>
<DATA null="true"/>
</MeasurementId>
</GoalMeasurementEORow>
</EO>
<EO Name="oracle.apps.hcm.goals.core.publicModel.entity.GoalMeasurementEO">
<![CDATA[00010000000EACED00057708000110D943311B2F]]>
<GoalMeasurementEORow PS="0" Hdl="1019" PK="Y" CI="Y" AV="">
<MeasurementId>
<DATA>300000297949999</DATA>
</MeasurementId>
<BusinessGroupId>
<DATA>1</DATA>
</BusinessGroupId>
</EO>
我需要使用 Oracle 在 table 中删除带有 <![CDATA[0001FFFFFFFF]]>
的 EO 节点。此 xml 存储在 XMLTYPE 的 XM_DATA_CACHE 中。
有没有办法根据CDATA
最终输出应该是
<EO Name="oracle.apps.hcm.goals.core.publicModel.entity.GoalMeasurementEO">
<![CDATA[0001FFFFFFFF]]>
<GoalMeasurementEORow PS="0" Hdl="16" PK="Y" CI="Y" AV="111111111111111111111111111111">
<MeasurementId>
<DATA null="true"/>
</MeasurementId>
</GoalMeasurementEORow>
</EO> ```
首先你的 xml 坏了:第二个标签 GoalMeasurementEORow
没有关闭,而且因为它只是 xml 的一部分,它没有根标签。
无论如何,我已经修复了你的 xml 以展示你如何从你的 xml.
中删除cdata
请看下面的例子:
-- sample data:
with t(x) as (
select xmltype(q'{
<ROOT>
<EO Name="oracle.apps.hcm.goals.core.publicModel.entity.GoalMeasurementEO">
<![CDATA[0001FFFFFFFF]]>
<GoalMeasurementEORow PS="0" Hdl="16" PK="Y" CI="Y" AV="111111111111111111111111111111">
<MeasurementId>
<DATA null="true"/>
</MeasurementId>
</GoalMeasurementEORow>
</EO>
<EO Name="oracle.apps.hcm.goals.core.publicModel.entity.GoalMeasurementEO">
<![CDATA[00010000000EACED00057708000110D943311B2F]]>
<GoalMeasurementEORow PS="0" Hdl="1019" PK="Y" CI="Y" AV="">
<MeasurementId>
<DATA>300000297949999</DATA>
</MeasurementId>
<BusinessGroupId>
<DATA>1</DATA>
</BusinessGroupId>
</GoalMeasurementEORow>
</EO>
</ROOT>
}')
from dual)-- end of sample data
-- main query:
select
XMLQuery(
'copy $i := $p1 modify
(for $j in $i//EO
let $o := <EO >{$j/@*}{ $j/node()[name() ne ""]}</EO>
return replace node $j with $o
)
return $i'
PASSING t.x AS "p1"
returning content
) xdata_new
from t;
结果:
XDATA_NEW
----------------------------------------------------------------------------------
<ROOT>
<EO Name="oracle.apps.hcm.goals.core.publicModel.entity.GoalMeasurementEO">
<GoalMeasurementEORow PS="0" Hdl="16" PK="Y" CI="Y" AV="111111111111111111111111111111">
<MeasurementId>
<DATA null="true"/>
</MeasurementId>
</GoalMeasurementEORow>
</EO>
<EO Name="oracle.apps.hcm.goals.core.publicModel.entity.GoalMeasurementEO">
<GoalMeasurementEORow PS="0" Hdl="1019" PK="Y" CI="Y" AV="">
<MeasurementId>
<DATA>300000297949999</DATA>
</MeasurementId>
<BusinessGroupId>
<DATA>1</DATA>
</BusinessGroupId>
</GoalMeasurementEORow>
</EO>
</ROOT>
如您所见,我正在用它们的内容替换所有子节点 EO
,CDATA 消失。