XML : Not certain how exactly to filter this simple FLWOR query in XQuery?

I honestly assumed I would not have many issues with this simple query, but I am supposed to write a query that displays which wards have the most gun-related crimes, but I have been stuck on just 1-2 lines of code and keep getting this error in XBase:

descendant::ward: node expected as input

I tried using "where" in place of the first "let", since I am limiting the results to "GUN" crimes, but then I get this error:

Incomplete FLWOR expression: expecting 'return'.

Here is my code:

    xquery version "1.0";  declare variable $crimes := doc('dc_crime.xml')//crime;    (:        Query to display total number of gun crimes by ward    <gunCrimes>{    for $ward in distinct-values(doc('dc_crime.xml'))//ward  let $details := $crimes[ward=$ward]   let $count := count($details)  order by $count descending    return   <count ward="{$ward}" crimes="{$count}">{$details}</count>  }</gunCrimes>    

Sample XML snip:

        <crime id="13086252">        <dateTime>2013-06-23T20:02:00</dateTime>        <month>6-Jun</month>        <day>1-Sun</day>        <offense>ASSAULT W/DANGEROUS WEAPON</offense>        <method>GUN</method>        <ward>8</ward>     </crime>    

I am still very new at XQuery, but can someone please enlighten me on what I am doing wrong? Thank you very much.

No comments:

Post a Comment