不确定如何在 XQuery 中过滤这个简单的 FLWOR 查询?
Not certain how exactly to filter this simple FLWOR query in XQuery?
老实说,我原以为这个简单的查询不会有太多问题,但我应该编写一个查询来显示哪些病房的枪支相关犯罪最多,但我只停留在 1-2 行代码并在 XBase 中不断收到此错误:
descendant::ward: node expected as input
我尝试使用 "where" 代替第一个 "let",因为我将结果限制为 "GUN" 犯罪,但随后出现此错误:
Incomplete FLWOR expression: expecting 'return'.
这是我的代码:
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>
示例 XML 片段:
<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>
我对 XQuery 还是很陌生,但是有人可以告诉我我做错了什么吗?非常感谢。
您可以将 crime
元素限制为谓词表达式中具有子 method
等于 "GUN"
的元素,例如:
.....
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
....
或者首先过滤全局变量:
declare variable $crimes := doc('dc_crime.xml')//crime[method='GUN'];
老实说,我原以为这个简单的查询不会有太多问题,但我应该编写一个查询来显示哪些病房的枪支相关犯罪最多,但我只停留在 1-2 行代码并在 XBase 中不断收到此错误:
descendant::ward: node expected as input
我尝试使用 "where" 代替第一个 "let",因为我将结果限制为 "GUN" 犯罪,但随后出现此错误:
Incomplete FLWOR expression: expecting 'return'.
这是我的代码:
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>
示例 XML 片段:
<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>
我对 XQuery 还是很陌生,但是有人可以告诉我我做错了什么吗?非常感谢。
您可以将 crime
元素限制为谓词表达式中具有子 method
等于 "GUN"
的元素,例如:
.....
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
....
或者首先过滤全局变量:
declare variable $crimes := doc('dc_crime.xml')//crime[method='GUN'];