如何在XQuery中比较两个数字(两次)并返回字符串
我有一个pub数据库,需要比较两个数字(纬度)和两个以上的数字(经度),然后返回位于这些坐标之间的每个pub名称如何在XQuery中比较两个数字(两次)并返回字符串,xquery,basex,Xquery,Basex,我有一个pub数据库,需要比较两个数字(纬度)和两个以上的数字(经度),然后返回位于这些坐标之间的每个pub名称 for $x in db:open("pub", "pub.xml")/serviceList/service where $x/geoData/latitude='40.400000000000' and $x/geoData/latitude='40.410000000000' return $x/basicData/name 我尝试了使用逻辑和算术运算符的where子句,尝试
for $x in db:open("pub", "pub.xml")/serviceList/service
where $x/geoData/latitude='40.400000000000' and $x/geoData/latitude='40.410000000000'
return $x/basicData/name
我尝试了使用逻辑和算术运算符的where子句,尝试了一个变量和两个变量
无论坐标如何,所有酒吧的输出总是相同的
for $x in db:open("pub", "pub.xml")/serviceList/service
where $x/geoData/latitude='40.400000000000' and $x/geoData/latitude='40.410000000000'
return $x/basicData/name
我们的想法是循环数据库,找到坐标在40.40到40.41(纬度)和-3.7 i-3.71(经度)之间的所有酒吧,并返回名称。您当前正在测试纬度值是否等于两个不同的字符串,并使用
和
标准(除非有多个latitude元素,否则它永远不会同时匹配这两个值,但您还没有展示XML的示例)
您可能希望将这些纬度和经度值作为数字进行比较(从值中删除引号)
以db表示的$x:open(“pub”、“pub.xml”)/serviceList/service
在哪里(
$x/geoData/latitude>=40.4和$x/geoData/latitude=-3.7和$x/geoData/latitudeMads Hansen的解决方案可以缩写为:
for $x in db:open("pub", "pub.xml")/serviceList/service
where (
$x/geoData/latitude[. ge 40.4 and . le 40.41]
and
$x/geoData/longitude[. ge -3.7 and . le 3.7]
)
return $x/basicData/name
甚至更进一步
db:open("pub", "pub.xml")/serviceList/service
[geoData[latitude[. ge 40.4 and . le 40.41] and longitude[. ge -3.7 and . le 3.7]]
/basicData/name
非常感谢,但至少对我来说不起作用。错误:类型xs:untypedAtomic和xs:decimal是不可比较的。在键入这篇文章之后,我一直在尝试,直到我最终使用下一个代码:for$x in db:open(“pub”,“pub.xml”)/serviceList/service let$lat:=$x/geoData/纬度let$long:=$x/geoData/经度where($lat>='40.40'和$lat=“-3.70”和($lon@Rex,如果您编写XQuery代码并想使用数字,那么就不要使用字符串文字,所以使用40.40
而不是'40.40'
,使用-3.70
(或者简单地说-3.7
)而不是“-3.70”
。至于Mads的建议,它可以使用类型化/验证的XML,其中latidude
是数字类型,但通常更容易使用通用比较运算符
或=
,因为它们可以处理不同类型的操作数,例如xs:untypedAtomic
使用$x/geoData/latitude>=40.4和$x/geoData/latitude是的,我知道,但我必须使用我得到的xml,这几乎是所有字符串。不过,我非常感谢你的建议。