1
votes

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.

1
Could you extend your sample enough to be a valid document, so we can actually test/run the query?Charles Duffy
There are also syntactical issues right now, like having a comment started but never closed. This is a failure to satisfy the "correctness" criteria in the meaning of sscce.org, which describes guidelines for building samples demonstrating a problem that others can use to help you.Charles Duffy
...also see stackoverflow.com/help/mcve for a similar resource.Charles Duffy
I guess, it is wrong closing bracket position at this part: distinct-values(doc('dc_crime.xml'))//ward, which should be written as : distinct-values(doc('dc_crime.xml')//ward)har07
@CharlesDuffy: Sorry, I posted a few questions with the exact same XML doc, so I felt bad about keep posting it and wanted to keep it simple. I have extended it. As for the comment, I am sorry but I had to delete my name and the book's name/chapter due to a bad experience in the past (got accused of cheating even though I wrote the code). I edited, thanks.Pau808

1 Answers

1
votes

You can limit the crime elements to those with child method equals "GUN" in the predicate expression, for example :

.....
for $ward in distinct-values(doc('dc_crime.xml')//ward)
(: notice the added `and method='GUN'` in the predicate :)
let $details := $crimes[ward=$ward and method='GUN'] 
let $count := count($details)
order by $count descending
....

or filtering the global variable in the first place :

declare variable $crimes := doc('dc_crime.xml')//crime[method='GUN'];