我有一个电影数据库的xml-schema:
<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>
</movies>
我现在创建了actor first / last_name的搜索字段;类型;国家;年和角色。
但问题是,它只适用于年份和类型。
如果我使用
流派或年份:我得到了正确的结果
姓氏;如first_name;国家;角色:我没有结果
我想它与多个演员和多个国家元素有关,但我无法弄清楚如何解决它。
这是我的Xquery代码:
xquery version "3.0";
(: :declare namespace util="http://exist-db.org/xquery/util";:)
declare option exist:serialize "method=xhtml media-type=text/html indent=yes";
let $year := lower-case(request:get-parameter('year', ''))
let $genre := request:get-parameter('genre', '')
let $first_name := request:get-parameter('first_name', '')
let $last_name := request:get-parameter('last_name', '')
let $country := request:get-parameter('country', '')
let $role := request:get-parameter('role', '')
let $movies := collection('/db/Movie/data')/movies/movie[if(not($genre)) then xs:boolean(1) else equals(genre, $genre)][if(not($year)) then xs:boolean(1) else equals(year, $year)][if(not($country)) then xs:boolean(1) else equals(//country, $country)][if(not($first_name)) then xs:boolean(1) else equals(//actor/first_name, $first_name)][if(not($last_name)) then xs:boolean(1) else equals(//actor/last_name, $last_name)][if(not($role)) then xs:boolean(1) else equals(//actor/role, $role)]
return
<html>
<head>
</head>
<body>
<h1>Search results:</h1>
<ol>{
for $movie in $movies
let $title := $movie/title/text()
let $year := $movie/year/text()
return
<li>{$title} ({$year})</li>
}</ol>
</body>
</html>
提前致谢! 迎接
答案 0 :(得分:0)
您的代码中存在拼写错误。在last_name的过滤谓词中,使用变量$ first_name。使用更多的行尾和空格可以更容易发现这样的错误。
您还可以使用更智能的布尔表达式使谓词更短。您可以将xs:boolean(1)
替换为true()
,但您也可以按如下方式重写谓词:
[empty($genre) or equals(genre, $genre)]
HTH!