【发布时间】:2014-01-11 16:24:39
【问题描述】:
我有一个电影数据库,想搜索具有姓氏和/或名字的演员。目标是获取演员姓名和头衔以及演员在电影中扮演的角色名称的列表。
XML 电影数据库如下所示:
<movies>
<movie>
<title>A History of Violence</title>
<year>2005</year>
<country>USA</country>
<genre>Crime</genre>
<summary>Tom Stall, a humble family man and owner of a
popular neighborhood restaurant, lives a quiet but
fulfilling existence in the Midwest. One night Tom
foils a crime at his place of business and, to his
chagrin, is plastered all over the news for his
heroics. Following this, mysterious people follow
the Stalls' every move, concerning Tom more than
anyone else. As this situation is confronted, more
lurks out over where all these occurrences have
stemmed from compromising his marriage, family
relationship and the main characters' former
relations in the process.</summary>
<director>
<last_name>Cronenberg</last_name>
<first_name>David</first_name>
<birth_date>1943</birth_date>
</director>
<actor>
<first_name>Vigo</first_name>
<last_name>Mortensen</last_name>
<birth_date>1958</birth_date>
<role>Tom Stall</role>
</actor>
<actor>
<first_name>Maria</first_name>
<last_name>Bello</last_name>
<birth_date>1967</birth_date>
<role>Eddie Stall</role>
</actor>
<actor>
<first_name>Ed</first_name>
<last_name>Harris</last_name>
<birth_date>1950</birth_date>
<role>Carl Fogarty</role>
</actor>
<actor>
<first_name>William</first_name>
<last_name>Hurt</last_name>
<birth_date>1950</birth_date>
<role>Richie Cusack</role>
</actor>
</movie>
实际上我有以下代码,到目前为止它可以工作,但例如对于带有 last_name=Dunst 的查询,我得到的结果是:
1. Dunst, Kirsten
movie title as role
2. Dunst, Kirsten
movie title as role
但我只想让演员有一次,所以我尝试添加 distinct-values() 但它不起作用:( 我想要这样的输出:
1. Dunst, Kirsten
movie title as role
movie title as role
代码如下:
xquery version "3.0";
declare option exist:serialize "method=xhtml media-type=text/html indent=yes";
let $last_name := distinct-values(request:get-parameter('last_name', ''))
let $first_name := distinct-values(request:get-parameter('first_name', ''))
let $movies := collection('/db/Movie/data')/movies/movie/actor[if(not($last_name)) then xs:boolean(1) else equals(last_name, $last_name)][if(not($first_name)) then xs:boolean(1) else equals(first_name, $first_name)]
return
<html>
<head>
</head>
<body>
<h1>Search results for actor {$last_name} {$first_name}:</h1>
<ol>{
for $movie in $movies
let $title := $movie/../title/text()
let $role := $movie/role/text()
return
<li>{$movie/last_name/text()}, {$movie/first_name/text()} <p> In the movie <i>{$title}</i> as role <i>{$role}</i> </p></li>
}</ol>
</body>
</html>
希望有人可以帮助我;) 提前致谢!
【问题讨论】:
-
底层数据是什么样的?
-
我从 XML 文件中添加了一个示例电影
-
你打算用它同时查询多个演员的名字/姓氏吗?