sort the query returnd value

i have the following document:

<key><landId>0012</landId> are other key elements......</key> are other elements......... 
</item> are other elements......... 

I want to get the sorted Item List of the order with its landId.
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.


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


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"
   for $i in $q/item sort by (name) return $i