2013-12-23 93 views
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> 

回答

1

你使用XQuery更新修改原始數據。你的查詢實際上不應該返回任何東西。

無論使用XQuery更新的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() 
}