我正在运行 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'm first foo !</foo>
<bar>I'm first bar !</bar>
</top>
2 <top><foo>I'm second foo !</foo><ba <top>
r>I'm second bar !</bar></top> <foo>I'm second foo !</foo>
<bar>I'm second bar !</bar>
</to
3 <top><foo>I'm third foo !</foo><bar <top>
>I'm third bar !</bar></top> <foo>I'm third foo !</foo>
<bar>I'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 查询生成器中:
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 不喜欢虚拟表吗?