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>
<crime id="13086254">
<dateTime>2013-06-23T20:56:00</dateTime>
<month>6-Jun</month>
<day>1-Sun</day>
<offense>THEFT/OTHER</offense>
<method>OTHERS</method>
<ward>2</ward>
</crime>
<crime id="13086257">
<dateTime>2013-06-23T20:52:00</dateTime>
<month>6-Jun</month>
<day>1-Sun</day>
<offense>THEFT/OTHER</offense>
<method>OTHERS</method>
<ward>7</ward>
</crime>
<crime id="13086268">
<dateTime>2013-06-23T19:54:00</dateTime>
<month>6-Jun</month>
<day>1-Sun</day>
<offense>ROBBERY</offense>
<method>GUN</method>
<ward>8</ward>
</crime>
<crime id="13086278">
<dateTime>2013-06-23T21:50:00</dateTime>
<month>6-Jun</month>
<day>1-Sun</day>
<offense>THEFT F/AUTO</offense>
<method>OTHERS</method>
<ward>1</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.
distinct-values(doc('dc_crime.xml'))//ward
, which should be written as :distinct-values(doc('dc_crime.xml')//ward)
– har07