This discussion is archived
2 Replies Latest reply: Dec 4, 2013 1:46 PM by Geraud RSS

How to add an element with a namespace prefix.

Geraud Newbie
Currently Being Moderated

I have a very simple problem, but I think I am going about it the wrong way.


I have the following XMLTYPE variable in pl/sql:

<A xmlns="namespace" xmlns:def="myns_namespace">


In a separate step, I would like to add the following element after B:

<C attr="attribute" def:defattr="def_attribute"/>


To produce the following xml:

<A xmlns="namespace" xmlns:def="myns_namespace">
     <C attr="attribute" def:defattr="def_attribute"/>


What is the best way to do this?  I have tried the following:

1.  The following fails because when creating the C element, oracle complains 'namespace prefix "def" is not declared.  This makes sense.

     xmlElement("C", xmlAttributes('attribute' "attr", 'def:defattr' "def_attribute")),
     'xmlns="namespace" xmlns:def="myns_namespace"'
INTO ...


2. I've also tried creating element C with the namespace attributes before adding to element A and then removing the namespace declaration attributes using deleteXML.  This does not work because apparently the function can delete Attributes, but it cannot delete namespace declarations. I am trying to avoid ending up with this:

<A xmlns="namespace" xmlns:def="myns_namespace">
     <C xmlns="namespace" xmlns:def="myns_namespace" attr="attribute" def:defattr="def_attribute"/>


I was hoping to find a function that could do any one of the following:

1. Create element fragments, ignoring namespace prefixes and not throw an error.

2. Remove specific namespace declarations or any redundant namespace declarations.

3. Add a child element some other way in which the child element would not need to be created and validated independently before being added to the parent.

4. Or maybe I am going about this whole xml creation process the wrong way and need to rethink my strategy?

  • 1. Re: How to add an element with a namespace prefix.
    odie_63 Guru
    Currently Being Moderated

    You'll have to do it in two steps in order to avoid namespace redeclarations at child level :

    SQL> SELECT xmlserialize(document

      2           insertchildxml(

      3             appendChildXML(

      4               xmltype('<A xmlns="namespace" xmlns:def="myns_namespace"><B/></A>')

      5             , '/A'

      6             , xmlelement("C", xmlattributes('attribute' as "attr"))

      7             , 'xmlns="namespace"'

      8             )

      9           , '/A/C'

    10           , '@def:attribute'

    11           , 'def_attribute'

    12           , 'xmlns="namespace" xmlns:def="myns_namespace"'

    13           )

    14           indent

    15         )

    16  FROM dual;




    <A xmlns="namespace" xmlns:def="myns_namespace">


      <C attr="attribute" def:attribute="def_attribute"/>



  • 2. Re: How to add an element with a namespace prefix.
    Geraud Newbie
    Currently Being Moderated

    Weird, I really thought I had tried this approach and it didn't work. I must not have commented out another line that I should have. This works, thank you.


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