XQuery:查找最大值并返回相关信息 [英] XQuery: find max value and return relevant information
问题描述
我有一个 XML 文件,其中包含具有以下结构的元素:
I have a XML file containing elements with the following structure:
<listing>
<auction_info>
<num_bids>29</num_bids>
</auction_info>
<item_info>
<result>item A</result>
</item_info>
</listing>
<listing>
<auction_info>
<num_bids>12</num_bids>
</auction_info>
<item_info>
<result>item B</result>
</item_info>
</listing>
我需要一个查询来计算 num_bids 的最大值并返回结果.在我的例子中,num_bids 29 是最大数目,所以需要返回项目 A.但在我下面的代码中,它返回了所有结果.
I need a query to calculate the max of num_bids and return the result.In my example,the num_bids 29 is the max number,so need return item A.But in my code below,it returned all results.
let $x := //listing
let $max := max($x/auction_info/num_bids)
return $x[//num_bids = $max]/item_info/result
感谢您的帮助.
推荐答案
你的表达要求的是给我所有 $x 文档中任何有 num_bids 的地方,任何地方,这等于 $max." 这与给我所有的 $x,其中 它自己的 num_bids 等于 $max."不同.以下应该做(未测试):
What your expression asks for is "give me all $x where there is a num_bids, anywhere in the document, which is equal to $max." Which is different than "give me all $x for which its own num_bids is equal to $max." The following should do (not tested):
let $in := //listing
let $max := max($in/auction_info/num_bids)
return
$in[auction_info/num_bids eq $max]/item_info/result
如果你真的想使用后代轴,你可以将 $x[//xxx]
更改为 $x[.//xxx]
(请参阅额外的点?)第一个说对于每个 $x,其根的任何 xxx 后代",第二个说对于每个 $x,它自身的任何 xxx 后代."但从您的示例输入来看,上面的查询很可能是您想要的.
If you really want to use the descendant axis, you can change $x[//xxx]
to $x[.//xxx]
(see the extra dot?) The first one says "for each $x, any xxx descendant from its root," the second says "for each $x, any xxx descendant from itself." But from your example input, the query above is most likely what you want.
这篇关于XQuery:查找最大值并返回相关信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!