Xquery FLWOR与分组依据 [英] Xquery FLWOR with group by

查看:118
本文介绍了Xquery FLWOR与分组依据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想按流派对所有电影进行分组,然后列出该流派​​中的所有电影标题.

I want to group all movies by genre and then list all movie titles in this genre.

我的XML电影数据库如下所示:

My XML movie database looks as following:

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

这是我的表情:

xquery version "3.0";

let $movie := collection ('/db/Movie/data')/movies/movie

return

<html>
<head>

     </head>
     <body>
        <h1>Movies grouped by genre:</h1>

        <ol>{
              for $m in $movie
              let $g := $m/genre
              let $t := distinct-values($m/title/text())
              group by $g 
  return
                <li>{$g}  <p> <ol>Title: {$t}</ol> </p></li>



        }</ol>
   </body>
</html> 

但结果将使我在同一行中获得所有标题,但我也希望它们也分开列出点数.

but the result will give me all titles in one row but I want them also als List points seperated.

这是实际输出:

<li>
<genre>Crime</genre>
<p>
<ol>Title: A History of Violence Heat Match Point</ol>
</p>
</li>
<li>

应如下所示:

<li>
<genre>Crime</genre>
<p>
<ol>Title: A History of Violence 
           Heat 
           Match Point
</ol>
</p>
</li>
<li>

我该如何调整查询?

先谢谢了. 问候

推荐答案

只需在其中添加另一个循环.我重新格式化并重命名了一些变量以使其更具描述性.通常,如果确实有很好的理由,请不要使用text(),在大多数情况下,最好使用data()代替,它会聚合元素内的所有文本节点.

Just add another loop inside. I did reformat and renamed some variables for being more descriptive. Generally, do not use text() if there's a really good reason to do so, most of the time it's better to use data() instead which aggregates all text nodes inside an element.

xquery version "3.0";

let $movies := collection ('/db/Movie/data')/movies/movie
return
  <html>
    <head></head>
    <body>
      <h1>Movies grouped by genre:</h1>
      <ol>{
        for $movie in $movies
        let $genre := $movie/genre
        group by $genre 
        let $titles := distinct-values($movie/title/data())
        return
          <li>
            <h2>{$genre} Titles</h2>
            <ol>{
              for $title in $titles
              return <li>{$title}</li>
            }</ol>
          </li>
        }</ol>
     </body>
   </html> 

您可以使用将元素构造函数用作轴步的隐式循环,但这需要删除distinct-values调用(您真的需要吗?),我只是重复了$movie循环:

You could use an implicit loop using an element constructor as axis step, but this would require to remove the distinct-values call (do you really need it?) I just repeated the $movie loop:

for $movie in $movies
let $genre := $movie/genre
group by $genre 
return
  <li>
    <h2>{$genre} Titles</h2>
    <ol>{ $movie/title/element li { data() } }</ol>
  </li>


顺便说一句,HTML不允许段落内的列表.无论如何,它都是格式正确的XML,但不是有效的HTML.我也解决了.


By the way, HTML does not allow lists inside paragraphs. It is well-formed XML anyway, but not valid HTML. I fixed that, too.

这篇关于Xquery FLWOR与分组依据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆