Oracle XMLQuery破坏名称空间 [英] Oracle XMLQuery is corrupting the namespace

查看:76
本文介绍了Oracle XMLQuery破坏名称空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Oracle版本11.2

Oracle version 11.2

下面是我在XMLType列上运行的XMLQuery的简化版本.当我运行查询时,只需解析并重新创建存储的XML,就会更改tsxm名称空间(,它不等于默认名称空间).该查询不执行任何操作,很容易被重写,但是真正的(更大)查询使用了相同的方法,因此这就是我以这种格式发布问题的原因.如果我将tsxm命名空间定义更改为与默认命名空间相同:

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 tsxm namespace (that is not equal to the default namespace ) gets changed. 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. If I change the tsxm namespace definition to be the same as the default namespace :

xmlns:tsxm="http://schemas.thomson.com/ts/20041221/tsip"

然后问题消失了,但是在实际应用中这是不可能的.

then the problem goes away, but in the real application this is not possible.

创建表:

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>

');

运行XMLQuery:

Run the 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?

So , the question is, what is causing the tsxm namespace declaration to be changed to syspfx_AT and the tsxm namespace prefix to xmlns:syspfx_1?

任何想法都值得赞赏.

推荐答案

根据 My Oracle,这似乎是预期的行为支持文档ID 2060374.1.但是,通过一些实验,似乎在XPath中对名称空间进行通配符阻止了这种情况的发生.所以:

This appears to be expected behaviour, according to My Oracle Support doc ID 2060374.1. But from a bit of experimentation it seems that wildcarding the namespace in your XPath stops it happening; so:

  <patent>{$m/*:patent/@*}
  {
    for $i in $m/*:patent/*
        return    $i
  }
  </patent>' 

使用原始数据(在11.2.0.4中),并进行序列化以使其更易读:

With your original data (in 11.2.0.4), and serialized to format it more readably:

 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>                                                                                                                                                                                                                             

这与您的原始名称不同,但是不再有名称空间损坏.您是否可以在实际查询中做到这一点,以及通配符是否会导致任何问题,这是另一回事...

That isn't identical to your original but doesn't have the namespace corruption any more. Whether you can do that in your real query, and whether the wildcarding causes any issues for that, are another matter...

这篇关于Oracle XMLQuery破坏名称空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆