Oracle 11.2
Below is a cut down version of an XMLQuery
i'm running on an XMLType
column. When I run the query, which simply parses and recreates the stored XML
, the unwanted default and tsip namespaces get inserted into the child elements of the parent. Note that the tsxm namespace does not get inserted, this is because it is not equal to the default namespace This query does nothing and could easily be rewritten, but the real (much bigger) query uses this same methodology so this is why i'm posting the question in this format.
create the table:
CREATE TABLE XML_DOCUMENT_TMP
(
DOCUMENT_ID NUMBER(12) NOT NULL,
XML_DATA SYS.XMLTYPE NOT NULL,
CREATED_DATE TIMESTAMP(6) NOT NULL
);
Insert some data (which must have the namespaces as is):
insert into XML_DOCUMENT_TMP
(document_id,created_date,xml_data)
values(1,sysdate,
'<patent xmlns="http://schemas.thomson.com/ts/20041221/tsip"
xmlns:tsip="http://schemas.thomson.com/ts/20041221/tsip"
xmlns:tsxm="http://schemas.thomson.com/ts/20041221/tsxm"
tsip:action="replace" tsip:cc="CA" tsip:se="2715340" tsip:ki="C">
<accessions tsip:action="replace">
<accession tsip:src="wila" tsip:type="key">CA-2715340-C</accession>
<accession tsip:src="tscm" tsip:type="tscmKey">CA-2715340-C-20150804</accession>
</accessions>
<claimed tsip:action="replace">
< claimsTsxm tsip:lang="en">
<tsxm:heading tsxm:align="left">We Claim:</tsxm:heading>
<claimTsxm tsip:no="1" tsxm:num="1" tsip:type="main">1. power. </claimTsxm>
</claimsTsxm>
</claimed>
</patent>
');
Run the XMLQuery
:
Note the need for namespace wildcarding is explained here
WITH tmpTable AS (
SELECT * FROM XML_DOCUMENT_TMP cm )
SELECT tt.xml_data ,
XMLQuery('declare default element namespace "http://schemas.thomson.com/ts/20041221/tsip";
declare namespace tsip="http://schemas.thomson.com/ts/20041221/tsip";
declare namespace tsxm="http://schemas.thomson.com/ts/20041221/tsxm";
return
<patent>{$m/*:patent/@*}
{
for $i in $m/*:patent/*
return $i
}
</patent>'
PASSING tt.xml_data as "m" RETURNING CONTENT) newXml
FROM tmpTable tt
WHERE tt.document_id in (1);
Returns:
<patent xmlns="http://schemas.thomson.com/ts/20041221/tsip" xmlns:tsip="http://schemas.thomson.com/ts/20041221/tsip" tsip:action="replace" tsip:cc="CA" tsip:se="2715340" tsip:ki="C">
<accessions xmlns="http://schemas.thomson.com/ts/20041221/tsip" xmlns:tsip="http://schemas.thomson.com/ts/20041221/tsip" tsip:action="replace">
<accession tsip:src="wila" tsip:type="key">CA-2715340-C</accession>
<accession tsip:src="tscm" tsip:type="tscmKey">CA-2715340-C-20150804</accession>
</accessions>
<claimed xmlns="http://schemas.thomson.com/ts/20041221/tsip" xmlns:tsip="http://schemas.thomson.com/ts/20041221/tsip" tsip:action="replace">
<claimsTsxm tsip:lang="en">
<tsxm:heading xmlns:tsxm="http://schemas.thomson.com/ts/20041221/tsip" tsxm:align="left">We Claim:</tsxm:heading>
<claimTsxm tsip:no="1" xmlns:tsxm="http://schemas.thomson.com/ts/20041221/tsip" tsxm:num="1" tsip:type="main">1. power.</claimTsxm>
</claimsTsxm>
</claimed>
How do I get rid of the unwanted namespaces created in the accessions and claimed elements. Any suggestions appreciated.