【问题标题】:Xquery distinct-values doesn´t workXquery distinct-values 不起作用
【发布时间】: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 文件中添加了一个示例电影
  • 你打算用它同时查询多个演员的名字/姓氏吗?

标签: xquery distinct-values


【解决方案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()

【讨论】:

  • 感谢您的回答,但我想获得演员的全名。例如,我只想使用 last_name=Dunst 进行搜索,但结果我想从 actor 元素中获得全名(即 last_name 和 first_name)。
  • 对,错过了。现已修复。
  • 完美! :) 非常感谢。
猜你喜欢
  • 2012-06-20
  • 2013-09-15
  • 2015-06-17
  • 1970-01-01
  • 2011-07-14
  • 1970-01-01
  • 2020-04-05
  • 1970-01-01
  • 2012-10-21
相关资源
最近更新 更多