2014-01-11 78 views
1

我有一個電影數據庫,並且想要搜索帶有最後名字和/或名字的演員。目標是列出演員的名字和標題以及演員在電影中扮演的角色名稱。Xquery distinct值不起作用

的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> 

其實我有下面的代碼,它到目前爲止的作品,但例如對於姓氏查詢=鄧斯特我得到的結果是:

1. Dunst, Kirsten 
movie title as role 
2. Dunst, Kirsten 
movie title as role 

但我想演員只有一次,所以我試圖添加不同的值(),但它並沒有工作:( 我想有這樣的輸出:

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

底層數據的外觀如何? – joemfb

+0

我已經從XML文件中添加了一個示例電影 – user3181473

+0

您是否打算使用它來同時查詢多個演員的名字/姓氏? – joemfb

回答

1

變量$moviesactor元素的序列綁定,這會導致一些混淆。

如果你只打算在同一時間使用此爲一個演員,你可以簡單地把該演員的名字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()

+0

感謝您的答案,但我想獲得完整的演員的名字。 例如,我想僅使用last_name = Dunst進行搜索,但結果是我想要從actors元素獲得全名(即last_name和first_name)。 – user3181473

+0

對,錯過了。現在修復。 – joemfb

+0

完美! :) 非常感謝你。 – user3181473