简体   繁体   中英

Custom sorting issue in MarkLogic?

xquery version "1.0-ml";
declare function local:sortit(){
  for $i in ('a','e','f','b','d','c')
  order by $i
  return
    element Result{
     element N{1},
     element File{$i}
    }
};

local:sortit()

the above code is sample, I need the data in this format. This sorting function is used multiple places, and I need only element N data some places and only File element data at other places.

But the moment I use the local:sortit()//File . It removes the sorting order and gives the random output. Please let me know what is the best way to do this or how to handle it.

All these data in File element is calculated and comes from multiple files, after doing all the joins and calculation, it will be formed as XML with many elements in it. So sorting using index and all is not possible here. Only order by clause can be used.

XPath expressions are always returned in document order.

You lose the sorting when you apply an XPath to the sequence returned from that function call.

If you want to select only the File in sorted order, try using the simple mapping operator ! , and then plucking the F element from the item as you are mapping each item in the sequence:

local:sortit() ! ./File

Or, if you like typing, you can use a FLWOR to iterate over the sequence and return the File :

for $result in local:sortit() 
return $result/File

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM