删除具有相同名称的多个xml节点(但不是全部)

时间:2016-06-17 12:24:46

标签: xml oracle10g

如何根据其中的数据删除xml节点。

例如,如果在下面我只想删除这两个

     <MMGroupID>143</MMGroupID>
     <MMGroupID>70</MMGroupID>  

<Promotion>
 <LastUpdated>2012-08-03T12:15:24+01:00</LastUpdated>
 <MajorVersion>1</MajorVersion>
 <MinorVersion>7</MinorVersion>
 <MultibuyGroup>
     <XMLSchemaVersion>1</XMLSchemaVersion>
     <UseLowestNetValue>0</UseLowestNetValue>
     <MMGroupID>975</MMGroupID>
     <MMGroupID>41</MMGroupID>
     <MMGroupID>143</MMGroupID>
     <MMGroupID>70</MMGroupID>
     <MMGroupID>36</MMGroupID>
     <MMGroupID>22</MMGroupID>
     <MMGroupID>870</MMGroupID>
     <MMGroupID>90</MMGroupID>
     <MMGroupID>88</MMGroupID>
     <MMGroupID>142</MMGroupID>
     <MMGroupID>147</MMGroupID>
     <TriggerOnLostSales>0</TriggerOnLostSales>
 </MultibuyGroup>

1 个答案:

答案 0 :(得分:0)

您可以将deleteXML()与指定所需节点值的XPath一起使用:

deleteXml(<your_xml>, q'{//MMGroupID[text()='143' or text()='70']}')

演示:

select XMLSerialize(document
  deleteXML(XMLType(
'<Promotion>
 <LastUpdated>2012-08-03T12:15:24+01:00</LastUpdated>
 <MajorVersion>1</MajorVersion>
 <MinorVersion>7</MinorVersion>
 <MultibuyGroup>
     <XMLSchemaVersion>1</XMLSchemaVersion>
     <UseLowestNetValue>0</UseLowestNetValue>
     <MMGroupID>975</MMGroupID>
     <MMGroupID>41</MMGroupID>
     <MMGroupID>143</MMGroupID>
     <MMGroupID>70</MMGroupID>
     <MMGroupID>36</MMGroupID>
     <MMGroupID>22</MMGroupID>
     <MMGroupID>870</MMGroupID>
     <MMGroupID>90</MMGroupID>
     <MMGroupID>88</MMGroupID>
     <MMGroupID>142</MMGroupID>
     <MMGroupID>147</MMGroupID>
     <TriggerOnLostSales>0</TriggerOnLostSales>
 </MultibuyGroup>
</Promotion>'),
  q'{//MMGroupID[text()='143' or text()='70']}')
  as CLOB INDENT size = 2)
from dual;

产生:

XMLSERIALIZE(DOCUMENTDELETEXML(XMLTYPE('<PROMOTION><LASTUPDATED>2012-08-03T12:15
--------------------------------------------------------------------------------
<Promotion>
  <LastUpdated>2012-08-03T12:15:24+01:00</LastUpdated>
  <MajorVersion>1</MajorVersion>
  <MinorVersion>7</MinorVersion>
  <MultibuyGroup>
    <XMLSchemaVersion>1</XMLSchemaVersion>
    <UseLowestNetValue>0</UseLowestNetValue>
    <MMGroupID>975</MMGroupID>
    <MMGroupID>41</MMGroupID>
    <MMGroupID>36</MMGroupID>
    <MMGroupID>22</MMGroupID>
    <MMGroupID>870</MMGroupID>
    <MMGroupID>90</MMGroupID>
    <MMGroupID>88</MMGroupID>
    <MMGroupID>142</MMGroupID>
    <MMGroupID>147</MMGroupID>
    <TriggerOnLostSales>0</TriggerOnLostSales>
  </MultibuyGroup>
</Promotion>