简体   繁体   中英

How to use DeleteXML - Need to Remove XML Node from XmlType in Oracle

I am using oracle 11.2.0.3. I have an XMLType column with the following structure:

<section>
    <question questionID="1">
        <answer>US</answer>
        <answer>XX</answer>
     </question>
</section>

I need to remove the second node such that after the update - I am left with the following:

<section>
    <question questionID="1">
        <answer>US</answer>
     </question>
</section>

I have been trying various queries without success. Based on my research, I would think that the query below would work - but it has not. I receive the message XX rows updated, but when I query the xml - the node I wanted to remove is still there! What am I missing here?

update SECTION s set s.section_xml = deleteXML(s.section_xml, '//question[@questionID=1]/answer[2]')

Thank you for any help!!!!!

Your query does work, at least on 11.2.0.2.0:

SQL> CREATE TABLE SECTION AS
  2  SELECT XMLTYPE('<section>
  3      <question questionID="1">
  4          <answer>US</answer>
  5          <answer>XX</answer>
  6       </question>
  7  </section>') section_xml FROM dual;

Table created

SQL> UPDATE SECTION s
  2     SET s.section_xml = deleteXML(s.section_xml,
  3                                   '//question[@questionID=1]/answer[2]');

1 row updated

SQL> SELECT s.section_xml.getClobVal() FROM SECTION s;

S.SECTION_XML.GETCLOBVAL()
--------------------------------------------------------------------------------
<section>
  <question questionID="1">
    <answer>US</answer>
  </question>
</section>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM