This discussion is archived
1 Reply Latest reply: Jun 21, 2012 7:39 AM by odie_63 RSS

How can I append an attribute to an xml data value?

user13176513 Newbie
Currently Being Moderated
I want to add an attribute that doesn't exist, something like

select appendxml(metadata, '/document/@newid', 2) from mytable where id = 1; <=== NO SUCH FUNCTION.

If the attribute already exists, you can call
select updatexml(metadata, '/document/@oldid', 1) from mytable where id = 1;

I don't want to have to do string manipulation to "splice in" the new attribute, then typecast it back to an xmltype. That's a lot of work, is error prone and has performance implications.

Thanks in advance.
  • 1. Re: How can I append an attribute to an xml data value?
    odie_63 Guru
    Currently Being Moderated
    The documentation provides all you need :
    SQL> with sample_data as (
      2    select xmltype('<document/>') metadata
      3    from dual
      4  )
      5  select insertchildxml(metadata, '/document', '@newid', '2')
      6  from sample_data
      7  ;
    <document newid="2"/>


  • Correct Answers - 10 points
  • Helpful Answers - 5 points