我有一个电影数据库,想要搜索姓氏和/或名字的演员。我们的目标是获得演员姓名和头衔,以及演员在电影中扮演的角色名称。
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>
希望有人可以帮助我;) 提前致谢!
答案 0 :(得分:1)
变量$movies
绑定到一系列actor
元素,这会引起一些混淆。
如果你打算一次只为一个演员使用它,你可以简单地将演员的名字放在FLWOR表达式之前,然后得到你想要的输出:
<ol>
<li>
<p>{ $movies[1]/last_name }, { $movies[1]/first_name}</p>
{
for $movie in $movies
let $title := $movie/../title
let $role := $movie/role
return
<p>In the movie <i>{$title}</i> as role <i>{$role}</i></p>
}</li>
</ol>
注意:在这种情况下,text()
路径选择器是不必要的,偶尔会混淆,因为它可以返回一系列文本节点。如果您需要确保类型约束,请考虑使用fn:string()
代替。