It's not clear what it means to "sort the above xml based on the value element 
for xml:lang="zh" or xml:lang="en" depending on some inputs". The structure 
includes multiple 'language' element, each with multiple language-annotated 
'value' elements. Do you want to sort the 'language' elements by 'value' and 
language?

If so, range indexes won't help because this will be an in-memory sort anyway. 
Range indexes are only used for very specific sorts, when the results are 
coming from the database and are aligned with the range index. See 
https://docs.marklogic.com/guide/performance/order_by for details.

If we're sorting in memory anyway there's no reason to set up a range index. 
The next hurdle is discussed in 
http://stackoverflow.com/questions/23769228/dynamic-sort-order-by-based-on-variable-in-xquery
 - the dynamic sort problem. Basically XQuery doesn't have a nice idiom for 
specifying dynamic sort keys. So you'll end up either building a string and 
calling xdmp:value, or enumerating every possible sort key. Neither option is 
ideal, but they both work.

Here's an example of how enumeration might work. First define the language 
elements as $list and set the value of $lang that you want to test:

let $list as element(language)+ := ...
let $lang as xs:string := 'en'

Then sort:

for $i in $list
order by
  if ($lang eq 'en') then $i/value[@xml:lang eq $lang] else ()
  empty least
  collation "http://marklogic.com/collation/en";,
  if ($lang eq 'zh') then $i/value[@xml:lang eq $lang] else ()
  empty least
  collation "http://marklogic.com/collation/zh";
return $i

You can change the behavior around empty values and tweak the collations. You 
*can't* use a variable as a collation string: for no good reason that I know 
of, collations in an order-by clause have to be string literals. So there has 
to be a separate sort key with its own if-then-else and collation for every 
distinct collation that you want to support. This gets tedious.

Or you could build a FLWOR as a string and call xdmp:value on it. This way we 
*can* use a variable for the collation string, but there'll be some overhead 
from calling xdmp:value and some risk of building an invalid FLWOR expression.

let $collation :=
  switch($lang)
  case 'en' return "http://marklogic.com/collation/en";
  case 'zh' return "http://marklogic.com/collation/zh";
  (: Add more cases as needed. :)
  default return error((), 'UNEXPECTED', $lang)
let $xqy := 'for $i in $list order by $i/value[@xml:lang eq $lang]'
  ||' collation "'||$collation||'"'
  ||' return $i'
return xdmp:value($xqy)

-- Mike

On 10 Jun 2014, at 23:56 , Ashish Bhardwaj <[email protected]> wrote:

> Hi David,
> 
> Thanks for your response. Here is the xml:
> <multilanguage>
> <language><value xml:lang="zh">丁2橡胶</value> <value 
> xml:lang="en">third</value></language>
> <language><value xml:lang="zh">丙酸异辛酯</value> <value 
> xml:lang="en">tenth</value></language>
> <language><value xml:lang="zh">丁烯</value> <value 
> xml:lang="en">first</value></language>
> <language> <value xml:lang="en">sixth</value></language>
> <language><value xml:lang="zh">丙酸 丁酯</value> <value 
> xml:lang="en">seventh</value></language>
> <language><value xml:lang="zh">丁苯胶</value> <value 
> xml:lang="en">fourth</value></language>
> <language><value xml:lang="en">eleventh</value></language>
> <language><value xml:lang="zh">丁1橡胶</value><value 
> xml:lang="en">second</value></language>
> <language><value xml:lang="zh">丙</value> <value 
> xml:lang="en">1</value></language>
> <language><value xml:lang="zh">丙~</value> <value 
> xml:lang="en">fifth</value></language>
> <language><value xml:lang="zh">丙 乙酯</value> <value 
> xml:lang="en">eighth</value></language>
> </multilanguage>
> 
> I need to sort the above xml based on the value element for xml:lang="zh" or 
> xml:lang="en" depending on some inputs.
> 
> How can I do this in MarkLogic.
> What FLWOR will be needed for both language sorting.
> 
> Also I am thinking to have to range indexes with different collations(1 for 
> zh and other for en) is this the right way to solve this.
> 
> Regards,
> Ashish
> 
> 
> On Tue, Jun 10, 2014 at 4:14 PM, David Ennis <[email protected]> wrote:
> HI.
> 
> Separate from language support, you can specify the collation to use on 
> specific string  indexes. 
> 
> https://docs.marklogic.com/guide/search-dev/encodings_collations
> 
> In my opinion, This is really the magic needed for granular control sorting.
> 
> As well as defaults, you can define the collation on the index and then refer 
> to a specific collation in your search.
> 
> Let me know if this helps.  Otherwise, provide an XML sample and how you want 
> it sorted.  Then there will be enough information for someone to provide a 
> proper sample.
> 
> Kind Regards,
> David Ennis
> 
> 
> On 10 June 2014 12:16, Ashish Bhardwaj <[email protected]> wrote:
> Hi,
> 
> Can anyone tell me what are the ways of doing a language specific sorting on 
> a particular data using Marklogic XQuery?
> 
> I am new to MarkLogic and have gone through a lot of documentation around 
> collations in Marklogic but how to run a query which can do language specific 
> sorting is something which is nowhere mentioned in documentation. I would 
> appreciate if someone can give me a few examples.
> 
> Thanks,
> Ashish
> 
> _______________________________________________
> General mailing list
> [email protected]
> http://developer.marklogic.com/mailman/listinfo/general
> 
> 
> 
> _______________________________________________
> General mailing list
> [email protected]
> http://developer.marklogic.com/mailman/listinfo/general
> 
> 
> _______________________________________________
> General mailing list
> [email protected]
> http://developer.marklogic.com/mailman/listinfo/general

_______________________________________________
General mailing list
[email protected]
http://developer.marklogic.com/mailman/listinfo/general

Reply via email to