Skip to Main Content

DevOps, CI/CD and Automation

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

How to add an element with a namespace prefix.

GeraudDec 4 2013 — edited Dec 4 2013

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">

     <B/>

</A>

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">

     <B/>

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

</A>

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.

SELECT

insertChildXML(

     eltA,

     '/A/B',

     'C',

     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">

     <B/>

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

</A>

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?

This post has been answered by odie_63 on Dec 4 2013
Jump to Answer

Comments

odie_63
Answer

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;

XMLSERIALIZE(DOCUMENTINSERTCHI

--------------------------------------------------------------------------------

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

  <B/>

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

</A>

Marked as Answer by Geraud · Sep 27 2020
Geraud

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.

1 - 2
Locked Post
New comments cannot be posted to this locked post.

Post Details

Locked on Jan 1 2014
Added on Dec 4 2013
2 comments
2,959 views