使用Oracle 11g BI Publisher创建XML数据的数据模型

时间:2011-05-30 08:23:05

标签: xml oracle oracle11g xmltype bi-publisher

我正在运行Oracle BI Publisher 11g(11.1.1.3.0),我正在尝试访问存储在数据库表中的XML数据。不幸的是,我有几个问题,目前我很困惑。

我有一张桌子:

create table xml_test(
  id number,
  data1 clob,
  data2 xmltype
);

desc xml_test
 Name                                         Null?    Type
 -------------------------------------------- -------- ---------------------------
 ID                                                    NUMBER
 DATA1                                                 CLOB
 DATA2                                                 PUBLIC.XMLTYPE

填写XML数据:

insert into xml_test values (
  1,
  to_clob(xmltype.createxml('<top><foo>I''m first foo !</foo><bar>I''m first bar !</bar></top>')),
  xmltype.createxml('<top><foo>I''m first foo !</foo><bar>I''m first bar !</bar></top>')
);
insert into xml_test values (
  2,
  to_clob(xmltype.createxml('<top><foo>I''m second foo !</foo><bar>I''m second bar !</bar></top>')),
  xmltype.createxml('<top><foo>I''m second foo !</foo><bar>I''m second bar !</bar></top>')
);
insert into xml_test values (
  3,
  to_clob(xmltype.createxml('<top><foo>I''m third foo !</foo><bar>I''m third bar !</bar></top>')),
  xmltype.createxml('<top><foo>I''m third foo !</foo><bar>I''m third bar !</bar></top>')
);

commit;

我可以用sqlplus查询它:

column id format 99
column data1 format a35
column data2 like data1

select * from xml_test;

 ID DATA1                               DATA2
--- ----------------------------------- -----------------------------------
  1 <top><foo>I'm first foo !</foo><bar <top>
    >I'm first bar !</bar></top>          <foo>I&apos;m first foo !</foo>
                                          <bar>I&apos;m first bar !</bar>
                                        </top>

  2 <top><foo>I'm second foo !</foo><ba <top>
    r>I'm second bar !</bar></top>        <foo>I&apos;m second foo !</foo>
                                          <bar>I&apos;m second bar !</bar>
                                        </to

  3 <top><foo>I'm third foo !</foo><bar <top>
    >I'm third bar !</bar></top>          <foo>I&apos;m third foo !</foo>
                                          <bar>I&apos;m third bar !</bar>
                                        </top>

select xt.id,
       xmlcast(xmlquery('//foo' passing xt.data2 returning content) as varchar2(30)) "FOO",
       xmlcast(xmlquery('//bar' passing xt.data2 returning content) as varchar2(30)) "BAR"
from xml_test xt;

 ID FOO                            BAR
--- ------------------------------ ------------------------------
  1 I'm first foo !                I'm first bar !
  2 I'm second foo !               I'm second bar !
  3 I'm third foo !                I'm third bar !

select xt.id, t.*
from xml_test xt,
     xmltable('//top' passing xt.data2
              columns "FOO" varchar2(15) path 'foo',
                      "BAR" varchar2(15) path 'bar') t;

 ID FOO             BAR
--- --------------- ---------------
  1 I'm first foo ! I'm first bar !
  2 I'm second foo  I'm second bar
  3 I'm third foo ! I'm third bar !

对我来说到目前为止一切看起来都很好,但是当我尝试在BI Publisher中创建数据模型时,我遇到了几个问题。

根据Oracle文档Using Data Stored as a Character Large Object (CLOB) in a Data Model,我应该能够在数据建模器中将data1列CLOB类型更改为XML类型。在我的安装中,我无法做到这一点,因为我从未提示文档中描述的下拉菜单。为什么我没有这个选项?表创建方式是错误的还是以错误的方式插入数据,还是以错误的方式安装或配置了BI Publisher或Oracle数据库?还是椅子和键盘之间的问题?但是,当我运行XML生成时,data1的值(正确地)显示为CLOB:

<ID>1</ID>
<DATA1>
<top><foo>I'm first foo !</foo><bar>I'm first bar !</bar></top>
</DATA1>
<DATA2/>

但是,列data2(即XMLTYPE的类型)根本不被识别为XML,但BI Publisher显示它是一个字符串,并在生成XML时返回null(参见上文)。

由于BI Publisher根本无法识别XMLTYPE,因此我尝试了一种解决方法。在BI Publisher Query Builder中,包含以下内容:

select "XML_TEST"."ID" as "ID",
       xmlcast(xmlquery('//foo' passing "XML_TEST"."DATA2" returning content) as varchar2(30)) as "FOO",
       xmlcast(xmlquery('//bar' passing "XML_TEST"."DATA2" returning content) as varchar2(30)) as "BAR"

 from  "XML_TEST" "XML_TEST"

按预期工作:

<ID_1>1</ID_1>
<BAR>I'm first bar !</BAR>
<FOO>I'm first foo !</FOO>

然而令人惊讶的是(对我而言)失败了:

/* This works on Query Builder but XML generation fails. */

select "xt"."ID" as "ID", t.bar_xml_test as "B1", t.foo_xml_test as "B2"
from   "XML_TEST" "xt",
       xmltable('//top' passing xt.data2
              columns "foo_xml_test" varchar2(15) path 'foo',
                      "bar_xml_test" varchar2(15) path 'bar') as t

Query Builder认为没关系,但XML生成失败了:

XML Parsing Error: no element found

为什么xmlcast + xmlquery有效,但xmltable却没有? BI Publisher不像虚拟表吗?

1 个答案:

答案 0 :(得分:0)

  

根据数据模型中的Oracle文档Using Data Stored as a Character Large Object (CLOB),我应该能够在数据建模器中将data1列CLOB类型更改为XML类型。在我的安装中,我无法做到这一点,因为我从未提示文档中描述的下拉菜单。

我的同事发现该功能仅在BI Publisher 11.1.1.5中可用,我们正在运行11.1.1.3。现在,当我们升级到11.1.1.5 the feature工作正常!