算在XQuery中给出奇怪的结果

时间:2012-12-05 06:27:35

标签: xml xquery

我正在计算在两个日期内到加利福尼亚州的旅行次数

我现有的XQuery如下:

for $x in doc("triptracker.xml")/data/cities/city,
  $y in doc("triptracker.xml")/data/trips/trip
let $date as xs:date := xs:date($y/date)
where $x/cityName = $y/cityName and $x/state = "CA"  and 
$date gt xs:date("2003-01-01") and $date lt xs:date("2006-12-31")
return <Result>Trips to California: {count($y)}</Result>

这给了我结果集

<Result>Trips to California: 1</Result>
<Result>Trips to California: 1</Result>
<Result>Trips to California: 1</Result>
<Result>Trips to California: 1</Result>
<Result>Trips to California: 1</Result>

而不只是给我一个5的计数

以下是为旅行和城市提供的XML数据类型

<trips>
    <trip>
        <ssn>123-20-4567</ssn>
        <cityName>Tucson</cityName>
        <date>2001-08-11</date>
    </trip>
</trips>
<cities>
    <city>
        <cityName>Houston</cityName>
        <state>TX</state>
        <location>
            <latitude>29.76045</latitude>
            <longitude>-95.369784</longitude>
        </location>
    </city>
</cities>

非常感谢任何帮助

1 个答案:

答案 0 :(得分:0)

我最后注意到通过使用fors,我永远不会只获得1个计数所以我使用let修改它

let $x := doc("triptracker.xml")/data/cities/city[state = "CA"],

$y := doc("triptracker.xml")/data/trips/trip[cityName=$x/cityName 
and xs:date(date) gt xs:date("2003-01-01")  
and xs:date(date) lt xs:date("2006-12-31")]

return <Result>Trips to California: {count($y)}</Result>