Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/amazon-s3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xml 如何在XQuery中获得计算值的最大值?_Xml_Max_Xquery - Fatal编程技术网

Xml 如何在XQuery中获得计算值的最大值?

Xml 如何在XQuery中获得计算值的最大值?,xml,max,xquery,Xml,Max,Xquery,我有很多订单如下: <order id="1"> <TargetPrice>100</TargetPrice> <Qty>2</Qty> </order> 100 2. 我需要一个Xquery来获取具有最高值的订单(其中值是TargetPrice*Qty) 如何才能做到这一点?按降序值对订单进行排序,并返回第一个元素 ( for $order in //order let $value := $or

我有很多订单如下:

<order id="1">
   <TargetPrice>100</TargetPrice>
   <Qty>2</Qty>
</order>

100
2.
我需要一个Xquery来获取具有最高值的订单(其中值是TargetPrice*Qty)


如何才能做到这一点?

按降序值对订单进行排序,并返回第一个元素

(
  for $order in //order
  let $value := $order/TargetPrice * $order/Qty
  order by $value descending
  return $order
)[1]

您可能希望实现函数和运算符3.1规范的D.6.1中的函数。这比“排序并获取第一个”解决方案要冗长一些,但它可能更快(原则上是可流化的),并且代码高度可重用

D.6.1例如:最高

函数eg:highest返回所提供函数中具有最高值的项

XSLT实现

declare function eg:highest(
                     $seq as item()*
                     $f as function(item()) as xs:anyAtomicType)
                  as item()* {
     fn:fold-left(
       fn:tail($seq), fn:head($seq),
       function($highestSoFar as item()*, $this as item()*) as item()* {
         let $thisValue := $f($this)
         let $highestValue := $f($highestSoFar[1])
         return
           if ($thisValue gt $highestValue)
             then $this
           else if ($thisValue eq $highestValue)
             then ($highestSoFar, $this)
           else $highestSoFar
       })
};
(剪报)

XQuery实现

declare function eg:highest(
                     $seq as item()*
                     $f as function(item()) as xs:anyAtomicType)
                  as item()* {
     fn:fold-left(
       fn:tail($seq), fn:head($seq),
       function($highestSoFar as item()*, $this as item()*) as item()* {
         let $thisValue := $f($this)
         let $highestValue := $f($highestSoFar[1])
         return
           if ($thisValue gt $highestValue)
             then $this
           else if ($thisValue eq $highestValue)
             then ($highestSoFar, $this)
           else $highestSoFar
       })
};
要查找工资最高的员工,可调用函数:

eg:highest(//employee, function($emp){$emp/salary})

另一个选项是使用并计算项目序列的
TargetPrice*Qty
的乘积,然后选择值,并在谓词中使用:

let $maxVal := max(//order ! (TargetPrice * Qty))
return //order[(TargetPrice * Qty) eq $maxVal]

所以我可以通过let变量进行排序?美好的谢谢大家!@Jens Erat如果有许多具有最高值的订单,那么答案是不正确的。事实上,这也适用于@MichaelKay的答案。虽然OP询问了一个订单。这会给你最高的V值,但你需要回顾订单,找到有V值的订单。是的,没有足够注意。我以为他要的是价值,而不是价值最高的