1

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.

6
  • Could you extend your sample enough to be a valid document, so we can actually test/run the query? Dec 5, 2015 at 4:46
  • 1
    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. Dec 5, 2015 at 4:47
  • 1
    ...also see stackoverflow.com/help/mcve for a similar resource. Dec 5, 2015 at 4:49
  • 1
    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
    Dec 5, 2015 at 4:55
  • @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
    Dec 5, 2015 at 5:00

1 Answer 1

1

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'];
1
  • 1
    Wow! I had no idea that you could simply filter the global variable like that. However, I suppose it makes sense, because they are for such things. Honestly, the book did not go into much detail with global variables, so I got most of my info from Google and you gentlemen! Thank you so very much, I learned a lot!
    – Pau808
    Dec 5, 2015 at 5:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.