Oracle 11.2版
以下是在XMLType列上运行的XMLQuery i的简化版本。当我运行只是解析并重新创建存储的XML的查询时,tsxm名称空间(不等于默认名称空间)会发生变化。此查询不执行任何操作,可以轻松地重写,但真正的(更大)查询使用相同的方法,因此这就是我以这种格式发布问题的原因。如果我将tsxm名称空间定义更改为与默认名称空间相同:
xmlns:tsxm="http://schemas.thomson.com/ts/20041221/tsip"
然后问题消失了,但在实际应用中这是不可能的。
创建表格:
CREATE TABLE XML_DOCUMENT_TMP
(
DOCUMENT_ID NUMBER(12) NOT NULL,
XML_DATA SYS.XMLTYPE NOT NULL,
CREATED_DATE TIMESTAMP(6) NOT NULL
);
插入一些数据:
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>
&#39);
运行XMLQuery:
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";
let $patsLus := $m/patent/*
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);
返回:
<patent xmlns="http://schemas.thomson.com/ts/20041221/tsip" xmlns:syspfx_AT="http://schemas.thomson.com/ts/20041221/tsip" syspfx_AT:action="replace" syspfx_AT:cc="CA" syspfx_AT:se="2715340" syspfx_AT:ki="C"><accessions xmlns="http://schemas.thomson.com/ts/20041221/tsip" action="replace">
<accession src="wila" type="key">CA-2715340-C</accession>
<accession src="tscm" type="tscmKey">CA-2715340-C-20150804</accession>
</accessions>
<claimed xmlns="http://schemas.thomson.com/ts/20041221/tsip" action="replace">
<claimsTsxm lang="en">
<syspfx_1:heading xmlns:syspfx_1="http://schemas.thomson.com/ts/20041221/tsxm" syspfx_1:align="left">We Claim:</syspfx_1:heading>
<claimTsxm no="1" xmlns:syspfx_1="http://schemas.thomson.com/ts/20041221/tsxm" syspfx_1:num="1" type="main">1. power.</claimTsxm>
</claimsTsxm>
</claimed>
</patent>
所以,问题是,是什么导致tsxm名称空间声明被更改为syspfx_AT,而tsxm名称空间前缀被更改为xmlns:syspfx_1?
任何想法都非常感激。
答案 0 :(得分:1)
根据My Oracle Support文档ID 2060374.1,这似乎是预期的行为。但是从一些实验中可以看出,在XPath中通配符名称空间可以阻止它的发生;这样:
<patent>{$m/*:patent/@*}
{
for $i in $m/*:patent/*
return $i
}
</patent>'
使用您的原始数据(在11.2.0.4中),并序列化以使其更易于格式化:
WITH tmpTable AS (
SELECT * FROM XML_DOCUMENT_TMP cm )
SELECT tt.xml_data ,
XMLSerialize(DOCUMENT
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";
let $patsLus := $m/patent/*
return
<patent>{$m/*:patent/@*}
{
for $i in $m/*:patent/*
return $i
}
</patent>'
PASSING tt.xml_data as "m" RETURNING CONTENT)
AS VARCHAR2(4000) INDENT SIZE = 2) ewXml
FROM tmpTable tt
WHERE tt.document_id in (1);
XML_DATA
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
NEWXML
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
<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" tsi
p: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>
<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/tsxm" tsxm:align="left">We Claim:</tsxm:heading>
<claimTsxm tsip:no="1" xmlns:tsxm="http://schemas.thomson.com/ts/20041221/tsxm" tsxm:num="1" tsip:type="main">1. power.</claimTsxm>
</claimsTsxm>
</claimed>
</patent>
这与您的原始版本不同,但不再有命名空间损坏。您是否可以在真实查询中执行此操作,以及通配符是否会导致任何问题,这是另一回事......