Xquery不同的值不´;行不通
我有一个电影数据库,想用姓和/或名搜索演员。目标是列出演员的名字、片名以及演员在电影中扮演的角色的名字 XML电影数据库如下所示:Xquery不同的值不´;行不通,xquery,distinct-values,Xquery,Distinct Values,我有一个电影数据库,想用姓和/或名搜索演员。目标是列出演员的名字、片名以及演员在电影中扮演的角色的名字 XML电影数据库如下所示: <movies> <movie> <title>A History of Violence</title> <year>2005</year> <country>USA</country> <genre>Crime</
<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>
1. Dunst, Kirsten
movie title as role
movie title as role
但是我只想让演员演一次,所以我尝试添加了distinct-values(),但它不起作用:(
我希望输出如下所示:
<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>
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>
xquery版本“3.0”;
声明选项exist:serialize“method=xhtml媒体类型=text/html缩进=yes”;
让$last_name:=不同的值(请求:获取参数('last_name','')
让$first\u name:=不同的值(请求:获取参数('first\u name','')
让$movies:=集合('/db/Movie/data')/movies/Movie/actor[if(not($last_name))然后xs:boolean(1)else等于(last_name,$last_name)][if(not($first_name))然后xs:boolean(1)else等于(first_name,$first_name)]
返回
参与者{$last\u name}{$first\u name}的搜索结果:
{
$movies中的$movie
让$title:=$movie/。/title/text()
让$role:=$movie/role/text()
返回
{$movie/last_name/text()},{$movie/first_name/text()}在电影中{$title}作为角色{$role}
}
希望有人能帮我;)
提前谢谢 变量
$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>
{$movies[1]/last_name},{$movies[1]/first_name}
{
$movies中的$movie
让$title:=$movie/。/title
让$role:=$movie/role
返回
在电影{$title}中扮演角色{$role}
}
注意:
text()
路径选择器在这种情况下是不必要的,有时会令人困惑,因为它会返回一系列文本节点。如果需要确保类型约束,请考虑使用<代码> fn:STRIGN()/<代码>。基础数据是什么样的?我从XML文件中添加了一个示例电影,您打算用它同时查询多个角色的第一个/最后一个名字?谢谢您的回答,但我想获得完整的演员名称。例如,我只想用last_name=Dunst搜索,但结果是我想从actors元素中获得全名(即last_name和first_name)。对,没有找到。现在修复。完美!:)非常感谢你。