插入后在xquery中排序

时间:2013-12-23 17:50:51

标签: sorting xquery

我希望按降序排列每辆车的总价值,

排序数据后,我想将属性级别添加到节点Car。

但是当我运行我的代码时,结果不是降序。

我的xml文件:

<Cars>
<Car TotalValue="27000">
<CarDetail Name="Peugot-206"/>
</Car>

<Car TotalValue="28000">
<CarDetail Name="Peugot-206"/>
</Car>

<Car TotalValue="30000">
<CarDetail Name="Peugot-206"/>
</Car>

<Car TotalValue="35000">
<CarDetail Name="Peugot-206"/>
</Car>

<Car TotalValue="270000">
<CarDetail Name="Peugot-206"/>
</Car>

<Car TotalValue="280000">
<CarDetail Name="Peugot-206"/>
</Car>

<Car TotalValue="300500">
<CarDetail Name="Peugot-206"/>
</Car>

<Car TotalValue="40000">
<CarDetail Name="Peugot-206"/>
</Car>

<Car TotalValue="270000">
<CarDetail Name="Peugot-206"/>
</Car>

<Car TotalValue="280000">
<CarDetail Name="Peugot-206"/>
</Car>

<Car TotalValue="3005000">
<CarDetail Name="Peugot-206"/>
</Car>

<Car TotalValue="400000">
<CarDetail Name="Peugot-206"/>
</Car>
</Cars>

我的代码:

let $sortResult := for $Car in doc('Process')//Car
                let $value:=number($Car/@TotalValue)
                order by $value descending 
               return $Car
for $sortItem at $position in $sortResult
return insert node (attribute Level {$position}) 
into $sortItem

运行结果:

<Cars>
<Car Level="12" TotalValue="27000">
 <CarDetail Name="Peugot-206"/>
</Car>

<Car Level="11" TotalValue="28000">
 <CarDetail Name="Peugot-206"/>
</Car>

<Car Level="10" TotalValue="30000">
 <CarDetail Name="Peugot-206"/>
</Car>

<Car Level="9" TotalValue="35000">
 <CarDetail Name="Peugot-206"/>
</Car>

<Car Level="6" TotalValue="270000">
  <CarDetail Name="Peugot-206"/>
</Car>

<Car Level="4" TotalValue="280000">
 <CarDetail Name="Peugot-206"/>
</Car>

<Car Level="3" TotalValue="300500">
 <CarDetail Name="Peugot-206"/>
</Car>

<Car Level="8" TotalValue="40000">
 <CarDetail Name="Peugot-206"/>
</Car>

<Car Level="7" TotalValue="270000">
  <CarDetail Name="Peugot-206"/>
</Car>

<Car Level="5" TotalValue="280000">
  <CarDetail Name="Peugot-206"/>
</Car>

<Car Level="1" TotalValue="3005000">
  <CarDetail Name="Peugot-206"/>
</Car>

<Car Level="2" TotalValue="400000">
 <CarDetail Name="Peugot-206"/>
</Car>

</Cars>

我需要的是什么:

<Cars>   
<Car Level="1" TotalValue="3005000">
 <CarDetail Name="Peugot-206"/>
</Car>

<Car Level="2" TotalValue="400000">
 <CarDetail Name="Peugot-206"/>
</Car>

<Car Level="3" TotalValue="300500">
 <CarDetail Name="Peugot-206"/>
</Car>

<Car Level="4" TotalValue="280000">
 <CarDetail Name="Peugot-206"/>
</Car>

<Car Level="5" TotalValue="280000">
 <CarDetail Name="Peugot-206"/>
</Car>

<Car Level="6" TotalValue="270000">
 <CarDetail Name="Peugot-206"/>
</Car>

 <Car Level="7" TotalValue="270000">
  <CarDetail Name="Peugot-206"/>
 </Car>

 <Car Level="8" TotalValue="40000">
   <CarDetail Name="Peugot-206"/>
 </Car>

 <Car Level="9" TotalValue="35000">
   <CarDetail Name="Peugot-206"/>
 </Car>

 <Car Level="10" TotalValue="30000">
   <CarDetail Name="Peugot-206"/>
 </Car>

 <Car Level="11" TotalValue="28000">
   <CarDetail Name="Peugot-206"/>
 </Car>

 <Car Level="12" TotalValue="27000">
  <CarDetail Name="Peugot-206"/>
 </Car>
 </Cars>

1 个答案:

答案 0 :(得分:1)

您正在使用XQuery Update修改原始数据。您的查询实际上根本不会返回任何内容。

使用XQuery Update的copy语句(您可以随后修改)或只是重新创建<Car/>个节点:

for $car at $position in (
  for $car in $xml//Car
  let $value := number($car/@TotalValue)
  order by $value descending
  return $car
)
return element Car {
  attribute Level { $position },
  $car/attribute(),
  $car/node()
}