This XQuery 1.0 solution is executable by any compatible XQuery 1.0 processor :
Note : No group by
and no distinct-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>
when applied to the provided XML document :
<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>
creates the desired, correct result :
<?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>
source share