database - find the max of an attribute per element in xml? -


i've been searching past hour on how find max of grouped element no success.

i'm trying report pid , uid , playcount of playlist highest playcount per user

here xml file:

<!doctype users system "users.dtd"> <users>  <user uid = "u1" dob = "06/03/94" email = "tom@hotmail.com">     <surname> doe</surname>     <givennames> jon </givennames>     <follows = "u1 u2"/>     <playlists>       <playlist pid = "p1" created ="12/03/11" playcount = "5" />      </playlists>    </user>    <user uid = "u2" dob = "06/03/95" email = "jane@hotmail.com">     <surname> dod</surname>     <givennames> jane </givennames>     <follows = "u1 u3"/>    </user>     <user uid = "u3" dob = "06/04/95" email = "dave@hotmail.com">     <surname> ron</surname>     <givennames> dave </givennames>     <follows = "u1 u2"/>     <playlists>       <playlist pid = "p3" created ="12/02/09" playcount = "9"/>        <playlist pid = "p9" created = "11/11/11" playcount = "11"/>      </playlists>    </user>     <user uid = "u4" dob = "06/04/99" email = "jeff@hotmail.com">     <surname> dun</surname>     <givennames> jeff</givennames>      <follows = "u1 u2 u3"/>     <playlists>       <playlist pid = "p4" created ="12/02/09" playcount = "3"/>        <playlist pid = "p6" created ="12/02/09" playcount = "55"/>     </playlists>    </user>  </users> 

i've tried doing

for $user in doc("users.xml")/users/user  $users/playlists/playlist/@playcount = max($users/playlists/playlist/@playcount)    return $user 

but doesn't work..nothing i've tried has worked...it either returns me max playcount per user (which best i've gotten since that's kinda idea of want out of query) or every playlist

i need query outputs:

<favorites>    <user uid = "u1" pid = "p1" playcount = "5"/>    <user uid = "u2" />    <user uid = "u3" pid = "p9" playcount = "11"/>    <user uid = "u4" pid = "p6" playcount = "55"/> </favorites> 

playlists optional element if user doesn't have playlists report uid..

any help? @ wits end here because seems hard find example of finding query uses max function grouped element..

i guess need branch cases, , customize output format. here xquery:

let $users := doc("users.xml")/users/user $user in $users let $maxcount := max($user/playlists/playlist/@playcount) let $maxplaylist := $user/playlists/playlist[@playcount = $maxcount] return if (count($maxplaylist) > 0) <user uid="{$user/@uid}" pid = "{$maxplaylist/@pid}" playcount = "{$maxplaylist/@playcount}"/> else <user uid="{$user/@uid}"/> 

Comments

Popular posts from this blog

tcpdump - How to check if server received packet (acknowledged) -