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