sort the query returnd value

Hi,
i have the following document:

<order> 
<key><landId>0012</landId>....here are other key elements......</key> 
.....here are other elements......... 
<item> 
<name>name1</name> 
</item> 
<item> 
<name>name2</name> 
</item> 
.....here are other elements......... 
</order>

I want to get the sorted Item List of the order with its landId.
question:
how can i sort the items by name?

I used the following xquery and have got an error message:sequernce occurrend where atomic value required.

for $q in input()/order 
where $q/key/landId='0012' 
sort by ($q/item/name) 
return <myOrder><land>{$q/key/landId}</land>{$q/item} </myOrder> 

How can i solve this problem?

Thank u in advance.

Chacha

if you look at the result of the query without a “sort” clause:


<myorder>
  <land><landid>0012</landid></land>
  <item><name>name2</name></item>
  <item><name>name1</name></item>
</myorder>

you see that there are two item/name nodes. The “sort” applies to the document and does not know which item/name node the documents should be sorted by.

To sort on the inner nodes the following xquery might work for you:


let $order := <order><key><landid>0012</landid></key><item><name>name3</name></item><item><name>name2</name></item><item><name>name1</name></item></order>
for $q in $order
where $q/key/landid = "0012"
return 
<myorder>
<land>{$q/key/landid}</land>
{
   for $i in $q/item sort by (name) return $i
}
</myorder>