我正在使用Oracle的XML DB来创建用户配置文件.我将用户配置文件存储在单个XMLTYPE列中,并在表中包含其他关系列(id,username,password). XML具有以下格式:
<profile> <subject>I <action>like <object>sports</object> ... <object>music</object </action> </subject> </profile>
我使用了以下查询,
SELECT * FROM user,XMLTABLE( '//profile' PASSING user.profile return COLUMNS action VARCHAR2(20) PATH '/subject/action',object VARCHAR2(30) PATH '/subject/action/object' );
这什么都没给我.我怎么能让这个东西工作?
您的示例上的操作和对象不在同一级别,因此您的查询必须执行其他步骤.这是一个例子:
原文链接:https://www.f2er.com/oracle/205674.htmlsql> create table users (id number,profile xmltype); Table created. sql> insert into users values (1,XMLTYPE('<profile> 2 <subject>I 3 <action>like 4 <object>sports</object> 5 <object>music</object> 6 </action> 7 </subject> 8 </profile>')); 1 row created. sql> select u.id,x.action,x.object.getStringVal() 2 from users u,3 XMLTABLE('/profile/subject/action' 4 passing u.profile 5 columns action VARCHAR2(30) PATH 'text()',6 object XMLTYPE PATH 'object') x; ID ACTION X.OBJECT.GETSTRINGVAL() --- ------- -------------------------------------------------- 1 like <object>sports</object> <object>music</object>
正如您所看到的,我们得到了节点,而不是您想要的,所以我们添加了一个XMLTABLE:
sql> select u.id,y.object 2 from users u,6 object XMLTYPE PATH 'object') x,7 XMLTABLE('/object' 8 passing x.object 9 columns object VARCHAR2(30) PATH '.') y; ID ACTION OBJECT --- ------- ------- 1 like sports 1 like music