-1

我是 XQuery 的新手,我的问题是关于不同的值,我正在使用以下代码来检索电影评论者

xquery version "1.0";
declare boundary-space preserve;
<result>
{for $reviews in doc("reviews.xml")/reviews/review,
     $movie in doc("movies.xml")/movies/movie
 where $reviews/movie_title = $movie/movie_title
   and $movie//movie_genre = "Drama"
   and $movie//month > 6
 order by $reviews/movie_reviewer descending
 return
(<reviewer>{distinct-values($reviews/movie_reviewer)}</reviewer>, '&#xa;')
 }
 </result>

后来我将代码更改为

xquery version "1.0";
declare boundary-space preserve;
<result>
{for $reviews in doc("reviews.xml")/reviews/review,
     $movie in doc("movies.xml")/movies/movie
 where $reviews/movie_title = $movie/movie_title
   and $movie//movie_genre = "Drama"
   and $movie//month > 6
 return
{for $content in distinct-values($reviews/movie_reviewer)
      order by $content descending
      return (<reviewer>{$content}</reviewer>, '&#xa;')}
 }
 </result>

但我得到了类似的结果

 <result>
 <reviewer>Wesley Barry</reviewer>
 <reviewer>Michael Gordon</reviewer>
 <reviewer>Michael Gordon</reviewer>
 <reviewer>Michael Gordon</reviewer>
 <reviewer>John Frankenheimer</reviewer>
 <reviewer>J. Lee Thompson</reviewer>
 <reviewer>J. Lee Thompson</reviewer>
 <reviewer>Charles Walters</reviewer>
 <reviewer>Charles Walters</reviewer>
 </result>

我怎样才能使结果像

<result>
 <reviewer>Wesley Barry</reviewer>
 <reviewer>Michael Gordon</reviewer>
 <reviewer>John Frankenheimer</reviewer>
 <reviewer>J. Lee Thompson</reviewer>
 <reviewer>Charles Walters</reviewer>
 </result>

? 我知道这是非常基本的,但我就是不明白这一点

4

1 回答 1

0

请遵循@Ranon 的建议。但是,从您的查询中猜测:最简单的方法是您是否可以使用 XQuery 3.0 和新的 group-by-statement。

xquery version "3.0";
declare boundary-space preserve;
<result>
{for $reviews in doc("reviews.xml")/reviews/review,
     $movie in doc("movies.xml")/movies/movie
 let $reviewer := $reviews/movie_reviewer
 where $reviews/movie_title = $movie/movie_title
   and $movie//movie_genre = "Drama"
   and $movie//month > 6
 group by $reviewer
 order by $reviewer descending
 return
(<reviewer>{$reviewer}</reviewer>, '&#xa;')
 }
 </result>
于 2012-11-18T13:16:33.603 回答