Skip to Main Content

SQL & PL/SQL

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!

XML namespace declaration repeated on too many elements

Jan VerveckenAug 1 2018 — edited Aug 1 2018

hi

Given the following XML, I would like to extract some elements and wrap them in another element for the same namespace.

<ex:myroot xmlns:ex="http://example.com">

  <ex:mystuff>

    <ex:myelem1>value1</ex:myelem1>

    <ex:myelem2>value2</ex:myelem2>

  </ex:mystuff>

</ex:myroot>

Should become :

<ex:myotherstuff xmlns:ex="http://example.com">

  <ex:myelem1>value1</ex:myelem1>

  <ex:myelem2>value2</ex:myelem2>

</ex:myotherstuff>

The following pl/sql code gives a "correct" result, but the namespace declaration xmlns:ex="http://example.com" is repeated on both elements ex:myelem1 and ex:myelem2 which is not required, and is what I would like to avoid.

declare

  l_input_xml XmlType := XmlType('

    <ex:myroot xmlns:ex="http://example.com">

      <ex:mystuff>

        <ex:myelem1>value1</ex:myelem1>

        <ex:myelem2>value2</ex:myelem2>

      </ex:mystuff>

    </ex:myroot>');

  l_my_elements_xml XmlType;

  l_output_xml XmlType;

begin

  l_my_elements_xml := l_input_xml.extract('/ex:myroot/ex:mystuff/*', 'xmlns:ex="http://example.com"');

  select XmlElement( "ex:myotherstuff"

                   , XmlAttributes ( 'http://example.com' as "xmlns:ex" )

                   , l_my_elements_xml

                   )

  into l_output_xml

  from dual;

  dbms_output.put_line('l_output_xml = ' || l_output_xml.getClobVal());

end;

/

Results in :

l_output_xml = <ex:myotherstuff xmlns:ex="http://example.com"><ex:myelem1 xmlns:ex="http://example.com">value1</ex:myelem1>

<ex:myelem2 xmlns:ex="http://example.com">value2</ex:myelem2>

</ex:myotherstuff>

Many thanks.

Regards,

Jan Vervecken

This post has been answered by mNem on Aug 1 2018
Jump to Answer
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Aug 29 2018
Added on Aug 1 2018
5 comments
477 views