Sorting 插入后xquery中的排序

Sorting 插入后xquery中的排序,sorting,xquery,Sorting,Xquery,我想按每辆车的总价值按降序排列 在对数据进行排序之后,我想向节点Car添加属性级别 但是当我运行代码时,结果不是按降序排列的 我的xml文件: <Cars> <Car TotalValue="27000"> <CarDetail Name="Peugot-206"/> </Car> <Car TotalValue="28000"> <CarDetail Name="Peugot-206"/> </Car> &

我想按每辆车的总价值按降序排列

在对数据进行排序之后,我想向节点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>
运行结果:

<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>

我需要什么:

<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>

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

使用XQuery Update的
copy
语句(您可以随后修改)或简单地重新创建
节点:

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()
}