質問

I am retrieving data and inserting it into another file. But the inserted data is not ordered. Following is the example of what I am trying to achieve.

let $n := <N>
<n>Lemon</n>
<n>Chickoo</n>
<n>Banana</n>
<n>Orange</n>
<n>Pineapple</n>
<n>Apple</n>
<n>Lemon</n>
<n>Chickoo</n>
<n>Banana</n>
</N>

for $x in distinct-values($n/n)
order by $x 
return 
    insert node  <n>{$x}</n> into doc('fruits')/fruit

The output of the fruits is as follows -

    <fruit>
            <n>Lemon</n>
            <n>Chickoo</n>
            <n>Banana</n>
            <n>Orange</n>
            <n>Pineapple</n>
            <n>Apple</n>
    </fruit>

Its not ordered, though mentioned while retrieving... !!!

役に立ちましたか?

解決

This is a bug in how BaseX evaluates the order by FLWOR clause, which is now documented on the GitHub bug tracker. The return clause is evaluated before sorting instead of afterwords, thus adding the updates in the wrong order.

A workaround would be to either do the sorting in another FLWOR expression:

for $x in
  for $d in distinct-values($n/n)
  order by $d
  return $d
order by $x 
return 
    insert node <n>{$x}</n> into doc('fruits')/fruit

Another (probably more elegant) solution is to use insert nodes, which only has to be evaluated once:

insert nodes
  for $x in distinct-values($n/n)
  order by $x 
  return <n>{$x}</n>
into doc('fruits')/fruit

他のヒント

It feels like a bug to me. The spec says:

If multiple nodes are inserted by a single insert expression, their order preserves the node ordering of the source expression.

which isn't exactly crystal-clear, but I think it means you have a right to expect the result to be properly sorted.

What implementation of XQuery Update are you using?

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top