xquery "last" 函数与其他选择器

xquery "last" function with other selectors

我很好奇如何在 Xquery 中使用 last() 函数以及其他 "selectors" 函数(不确定行话是否正确)。这是我所拥有的(并且有效):

for $lastscoreplay in //bbgame/plays/period[last()]/play[@action="GOOD"]

return <datarow>

<lastscoreplay>{data($lastscoreplay/@uni)}</lastscoreplay>

</datarow>

但是,我想 select 最后一个 play,其中 @action 等于 GOOD。如果这是有道理的。我想我可以这样做,但它不起作用:

for $lastscoreplay in //bbgame/plays/period[last()]/play[last()][@action="GOOD"]

return <datarow>

<lastscoreplay>{data($lastscoreplay/@uni)}</lastscoreplay>

</datarow>

抱歉,我对 Xquery 和一般编码还是比较陌生,如果这很简单,我很抱歉,我只是没有看到问题。感谢您的帮助!

你查询 get's last play,不管 @action,然后通过 [@action="GOOD"] 过滤最后播放。相反,您应该先按 @action 过滤,然后获取最后一个:

 //bbgame/plays/period[last()]/play[@action="GOOD"][last()]

完整查询:

for $lastscoreplay in //bbgame/plays/period[last()]/play[@action="GOOD"][last()]
return
  <datarow>
    <lastscoreplay>{ data($lastscoreplay/@uni) }</lastscoreplay>
  </datarow>

在某些情况下,接受的答案将不起作用,如果发生这种情况,请尝试以下替代方法:

let $all := /bbgame/plays/period[last()]/play[@action="GOOD"]
return $all[last()]