使用 XPath 查询 oracle 数据库时,如何 return 值列表而不是字符串?
How do I return a list of values instead of a string when querying a oracle database using XPath?
我正在使用 XPath 查询 oracle 数据库,其中我查询的字段如下所示:
<!-- language: lang-xml -->
<film>
<title>Godfather, The</title>
<year>1972</year>
<directors>
<director>Francis Ford Coppola</director>
</directors>
<genres>
<genre>Crime</genre>
<genre>Drama</genre>
</genres>
<plot>Son of a mafia boss takes over when his father is critically wounded in a mob hit.</plot>
<cast>
<performer>
<actor>Marlon Brando</actor>
<role>Don Vito Corleone</role>
</performer>
<performer>
<actor>Al Pacino</actor>
<role>Michael Corleone</role>
</performer>
<performer>
<actor>Diane Keaton</actor>
<role>Kay Adams Corleone</role>
</performer>
<performer>
<actor>Robert Duvall</actor>
<role>Tom Hagen</role>
</performer>
<performer>
<actor>James Caan</actor>
<role>Sonny Corleone</role>
</performer>
</cast>
</film>
我想return所有出演过《教父》的演员。目前我的代码如下所示:
result = stmt.executeQuery("SELECT a.FILM.extract('/film[title=\"Godfather, The\"]/cast/performer/actor/text()') "
+ "FROM ASS2_Film a "
+ "WHERE a.film.existsNode('/film[title=\"Godfather, The\"]')=1");
System.out.println("\nActor");
while (result.next()) {
System.out.println(result.getString(1)+"\n");
}
此刻我的代码是 returning:
Actor
Marlon BrandoAl PacinoDiane KeatonRobert DuvallJames Caan
如我所愿 return编辑为:
Actor
Marlon Brando
Al Pacino
Diane Keaton
Robert Duvall
James Caan
感谢您的帮助
EXTRACT
(和 EXTRACTVALUE
)是已弃用的函数。您应该改用 XMLTABLE
:
with sample_data as (select xmltype('<film>
<title>Godfather, The</title>
<year>1972</year>
<directors>
<director>Francis Ford Coppola</director>
</directors>
<genres>
<genre>Crime</genre>
<genre>Drama</genre>
</genres>
<plot>Son of a mafia boss takes over when his father is critically wounded in a mob hit.</plot>
<cast>
<performer>
<actor>Marlon Brando</actor>
<role>Don Vito Corleone</role>
</performer>
<performer>
<actor>Al Pacino</actor>
<role>Michael Corleone</role>
</performer>
<performer>
<actor>Diane Keaton</actor>
<role>Kay Adams Corleone</role>
</performer>
<performer>
<actor>Robert Duvall</actor>
<role>Tom Hagen</role>
</performer>
<performer>
<actor>James Caan</actor>
<role>Sonny Corleone</role>
</performer>
</cast>
</film>') x from dual)
select x.*
from sample_data sd,
xmltable('/film[title="Godfather, The"]/cast/performer' passing sd.x
columns actor varchar2(50) path '//actor',
role varchar2(50) path '//role') x;
ACTOR ROLE
-------------------------------------------------- --------------------------------------------------
Marlon Brando Don Vito Corleone
Al Pacino Michael Corleone
Diane Keaton Kay Adams Corleone
Robert Duvall Tom Hagen
James Caan Sonny Corleone
(我包含角色列只是为了提供更多信息;如果不需要,您只需从 XMLTABLE
部分的列列表中删除该列即可。)
我正在使用 XPath 查询 oracle 数据库,其中我查询的字段如下所示:
<!-- language: lang-xml -->
<film>
<title>Godfather, The</title>
<year>1972</year>
<directors>
<director>Francis Ford Coppola</director>
</directors>
<genres>
<genre>Crime</genre>
<genre>Drama</genre>
</genres>
<plot>Son of a mafia boss takes over when his father is critically wounded in a mob hit.</plot>
<cast>
<performer>
<actor>Marlon Brando</actor>
<role>Don Vito Corleone</role>
</performer>
<performer>
<actor>Al Pacino</actor>
<role>Michael Corleone</role>
</performer>
<performer>
<actor>Diane Keaton</actor>
<role>Kay Adams Corleone</role>
</performer>
<performer>
<actor>Robert Duvall</actor>
<role>Tom Hagen</role>
</performer>
<performer>
<actor>James Caan</actor>
<role>Sonny Corleone</role>
</performer>
</cast>
</film>
我想return所有出演过《教父》的演员。目前我的代码如下所示:
result = stmt.executeQuery("SELECT a.FILM.extract('/film[title=\"Godfather, The\"]/cast/performer/actor/text()') "
+ "FROM ASS2_Film a "
+ "WHERE a.film.existsNode('/film[title=\"Godfather, The\"]')=1");
System.out.println("\nActor");
while (result.next()) {
System.out.println(result.getString(1)+"\n");
}
此刻我的代码是 returning:
Actor
Marlon BrandoAl PacinoDiane KeatonRobert DuvallJames Caan
如我所愿 return编辑为:
Actor
Marlon Brando
Al Pacino
Diane Keaton
Robert Duvall
James Caan
感谢您的帮助
EXTRACT
(和 EXTRACTVALUE
)是已弃用的函数。您应该改用 XMLTABLE
:
with sample_data as (select xmltype('<film>
<title>Godfather, The</title>
<year>1972</year>
<directors>
<director>Francis Ford Coppola</director>
</directors>
<genres>
<genre>Crime</genre>
<genre>Drama</genre>
</genres>
<plot>Son of a mafia boss takes over when his father is critically wounded in a mob hit.</plot>
<cast>
<performer>
<actor>Marlon Brando</actor>
<role>Don Vito Corleone</role>
</performer>
<performer>
<actor>Al Pacino</actor>
<role>Michael Corleone</role>
</performer>
<performer>
<actor>Diane Keaton</actor>
<role>Kay Adams Corleone</role>
</performer>
<performer>
<actor>Robert Duvall</actor>
<role>Tom Hagen</role>
</performer>
<performer>
<actor>James Caan</actor>
<role>Sonny Corleone</role>
</performer>
</cast>
</film>') x from dual)
select x.*
from sample_data sd,
xmltable('/film[title="Godfather, The"]/cast/performer' passing sd.x
columns actor varchar2(50) path '//actor',
role varchar2(50) path '//role') x;
ACTOR ROLE
-------------------------------------------------- --------------------------------------------------
Marlon Brando Don Vito Corleone
Al Pacino Michael Corleone
Diane Keaton Kay Adams Corleone
Robert Duvall Tom Hagen
James Caan Sonny Corleone
(我包含角色列只是为了提供更多信息;如果不需要,您只需从 XMLTABLE
部分的列列表中删除该列即可。)