1
我想每一輛通過的總價值atrribute以降序排序後,XQuery中的排序插入
數據排序後,我想添加屬性級別節點租車。
但是當我運行我的代碼結果不是按降序排列。
我的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>