gpt4 book ai didi

xquery - Xquery中的分组和计数

转载 作者:行者123 更新时间:2023-12-04 13:14:34 25 4
gpt4 key购买 nike

听说是XML。我正在尝试获取作者发布的标题的数量,日期范围为 2012年2月15日至2012年2月24日,从高到低(标题数)。

<entries>
<entry>
<id>1</id>
<published>23/02/2012</published>
<title>Title 1</title>
<content type="html">This is title one</content>
<author>
<name>Pankaj</name>
</author>
</entry>
<entry>
<id>2</id>
<published>22/02/2012</published>
<title>Title 2</title>
<content type="html">This is title two</content>
<author>
<name>Pankaj</name>
</author>
</entry>
<entry>
<id>3</id>
<published>21/02/2012</published>
<title>Title 3</title>
<content type="html">This is title three</content>
<author>
<name>Rob</name>
</author>
</entry>
<entry>
<id>4</id>
<published>20/02/2012</published>
<title>Title 4</title>
<content type="html">This is title four</content>
<author>
<name>Bob</name>
</author>
</entry>
<entry>
<id>5</id>
<published>19/02/2012</published>
<title>Title 1</title>
<content type="html">This is title five</content>
<author>
<name>Pankaj</name>
</author>
</entry>

我正在尝试从xquery获取输出:
<?xml version="1.0" encoding="UTF-8"?>
<results>
<result>
<author>
<name>Pankaj</name>
</author>
<numberOfTitles>3</numberOfTitles>
</result>
<result>
<author>
<name>Rob</name>
</author>
<numberOfTitles>1</numberOfTitles>
</result>
<result>
<author>
<name>Bob</name>
</author>
<numberOfTitles>1</numberOfTitles>
</result>

请帮我..

最佳答案

此XQuery 1.0解决方案可由任何兼容的XQuery 1.0处理器执行:

注意:不使用group bydistinct-values()

<results> 
{
let $entries :=
/*/entry
[for $d in
xs:date(string-join(reverse(tokenize(published, '/')), '-'))
return
xs:date('2012-02-15') le $d and $d le xs:date('2012-02-24')
],

$vals := $entries/author/name
return
for $a in $vals[index-of($vals, .)[1]],
$cnt in count(index-of($vals, $a))
order by $cnt descending
return
<result>
<author>
{$a}
</author>
<numberOfTitles>
{count(index-of($vals, $a))}
</numberOfTitles>
</result>
}
</results>

应用于提供的XML文档时 :
<entries>
<entry>
<id>1</id>
<published>23/02/2012</published>
<title>Title 1</title>
<content type="html">This is title one</content>
<author>
<name>Pankaj</name>
</author>
</entry>
<entry>
<id>2</id>
<published>22/02/2012</published>
<title>Title 2</title>
<content type="html">This is title two</content>
<author>
<name>Pankaj</name>
</author>
</entry>
<entry>
<id>3</id>
<published>21/02/2012</published>
<title>Title 3</title>
<content type="html">This is title three</content>
<author>
<name>Rob</name>
</author>
</entry>
<entry>
<id>4</id>
<published>20/02/2012</published>
<title>Title 4</title>
<content type="html">This is title four</content>
<author>
<name>Bob</name>
</author>
</entry>
<entry>
<id>5</id>
<published>19/02/2012</published>
<title>Title 1</title>
<content type="html">This is title five</content>
<author>
<name>Pankaj</name>
</author>
</entry>
</entries>

产生所需的正确结果:
<?xml version="1.0" encoding="UTF-8"?>
<results>
<result>
<author>
<name>Pankaj</name>
</author>
<numberOfTitles>3</numberOfTitles>
</result>
<result>
<author>
<name>Rob</name>
</author>
<numberOfTitles>1</numberOfTitles>
</result>
<result>
<author>
<name>Bob</name>
</author>
<numberOfTitles>1</numberOfTitles>
</result>
</results>

关于xquery - Xquery中的分组和计数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9401252/

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