我试图实现的是将$stationsSequence中的值添加到新序列$newSequence中,然后在序列中返回distinct-values。我“天真”地尝试使用如下的if语句来完成此操作:
let $stationsSequence := fn:doc("stations.xml")//station
let $newSequence := ()
for $station in $stationsSequence
return
if ( fn:starts-with($station, "W") or fn:starts-with($station, "B") )
then
fn:insert-before($newSequence , last(), $station)
else ()
fn:distinct-values($newSequence)这样做的正确方法是什么?谢谢
发布于 2016-05-27 00:58:47
您不能在XQuery中更新变量,因为变量是不可变的。这就是说:
let $s := ()
return
let $s1 := fn:insert-before($s, last(), "a")
let $s2 := fn:insert-before($s, last(), "b")
return
$s2给出结果:
("b")相反,您必须创建一个包含旧值和新值的新变量,然后重复该操作,直到获得所有值。
然而,我认为对你的问题最简单的解决方案是:
fn:distinct-values(
fn:doc("stations.xml")//station[fn:substring(., 1, 1) = ("W", "B")]
)https://stackoverflow.com/questions/37464990
复制相似问题