On Fri, 13 Jun 2014 00:59:20 -0700, Ashish Bhardwaj <[email protected]> 
wrote:

> Hello Mike,
>
> Thanks a lot for your reply.
>
> I am storing data in Marklogic using the xml I mentioned before. Here is an
> simplified version of xml that I am storing in ML:
> <language>
>     <value xml:lang="en">English value-1</value>
>     <value xml:lang="zh">chines word-1</value>
> </language>
>
> I want to sort in either "en" or "zh" language. I think the below statement
> would work for me:
> let $lang := (: language to be used in sorting :)
>
> for $i in (: some cts to get data from ML :)
> order by  $i/value[@xml:lang eq $lang]
>
> Please suggest how to go with its indexing. Which index is most prominent
> in order to sort in both language.
>
> Sorry for my ignorance if I am lacking in some most basic information.

If you want to select the language-specific collation for
the language in question, it will be a little tricky to do
dynamically, because collations in XQuery for FLWOR statements
are statically given, either as part of the order by clause or as the
default collation for the query as a whole:


for $i in whatever
order by $i/value[@xml:lang=$lang]
       ascending collation "http://marklogic.com/collation/zh";

or

declare default collation "http://marklogic.com/collation/en";;

for $i in whatever
order by $i/value[@xml:lang=$lang]


So you could either duplicate code:

if ($lang="zh") then
    for $i in whatever
    order by $i/value[@xml:lang=$lang]
       ascending collation "http://marklogic.com/collation/zh";
else if ($lang="en") then
    for $i in whatever
    order by $i/value[@xml:lang=$lang]
       ascending collation "http://marklogic.com/collation/en";
else
    for $i in whatever
    order by $i/value[@xml:lang=$lang]
       ascending collation "http://marklogic.com/collation/";

Or you could use xdmp:eval, constructing the string
of the query and executing it. This comes with all the
potential risks of evaluating code strings (query injection,
etc.):

xdmp:eval('
    declare variable $lang as xs:string external;

    for $i in whatever
    order by $i/value[@xml:lang=$lang]
       ascending collation "http://marklogic.com/collation/' ||
    $lang || '"',
    ("lang", ($lang,"")[1])
)


For many purposes, the root collation
(http://marklogic.com/collation/";) is going to do a
fine job across languages. It depends on how many
language customizations the collation has, and how
much you care about that. For English, you lose the
ability to handle the ae ligature exactly right.
Chinese, which you specifically references, does have
a lot of customizations, so that would be an issue.

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

Reply via email to