gpt4 book ai didi

Xquery FLWOR 与 group by

转载 作者:行者123 更新时间:2023-12-01 15:02:20 26 4
gpt4 key购买 nike

我想按流派对所有电影进行分组,然后列出该流派​​中的所有电影片名。

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

这是我的表达:
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>

但结果会给我所有的标题在一行中,但我也希望它们也将列表点分开。

这是实际输出:
<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()相反,它聚合了一个元素内的所有文本节点。

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环形:
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。我也修好了。

关于Xquery FLWOR 与 group by,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21064189/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com