[xquery-talk] Problems with string testing in if

Michael Kay mhk at mhk.me.uk
Tue Jun 1 09:16:26 PDT 2004

> Thank you both for the help. Unfortunately none of the suggestions 
> worked, although they changed the behaviour of the function: now it 
> always returns "test" (except when called with the locale no_NO from 
> Safari (MacOS X), then it returns "nor" as expected; the same locale 
> from Mozilla returns "test", though). That is, in all but one case it 
> goes directly to the last else clause.

1. It would be a good idea to tell us which XQuery implementation you are
using. The drafts are still changing and different products implement
different versions.

2. You haven't really explained what the value of the parameter to this
function is. Can you give us a table that shows the input value ($locale)
and the result you are getting?

3. The code now looks correct to me. Both Jason and I supplied correct
solutions to your coding error. I suspect the next bug is in the code that
calls this function.

4. Personally, I would code this using a lookup table: define an XML
structure containing the input strings and output strings, and use an XPath
expression to access into this lookup table.

5. It's legitimate for different implementations to give you different
answers if they use a different default collation. For example, some
products might use a default collation that's case-blind (i.e. "no" = "NO").

Michael Kay

> I have attached the updated function below, for reference.
> Sjur
> (: Convert incoming locale strings to 3-letter language codes 
> (ISO 639) 
> :)
> declare function fn:locale2lang($locale as xs:string) as xs:string
> {
> 	let $lang := substring-before($locale,'_')
> 	return
> 		if ($lang = "no" or $lang = "nn" or $lang = 
> "nb" ) then "nor"
> (:		if ($lang = ("no", "nn", "nb") ) then "nor" :)
> 		else if ($lang = "se") then "sme"
> 		else if ($lang = "sv") then "swe"
> 		else if ($lang = "fi") then "fin"
> 		else if ($lang = "en") then "eng"
> 		else if ($lang = "smi") then "sme"
> 		else if ($lang = "sme" or $lang = "sma" or 
> $lang = "smn" or $lang =
> 					   "smj" or $lang = 
> "sms" or $lang = "sme" or $lang =
> 					   "nor" or $lang = 
> "swe" or $lang = "fin" or $lang = "eng") then 
> $lang
> (:		else if ($lang = ("sme", "sma", "smn",
> 					    "smj", "sms", "sme",
> 					    "nor", "swe", 
> "fin", "eng") ) then $lang :)
> 		else "test"
> };
> _______________________________________________
> talk at xquery.com
> http://xquery.com/mailman/listinfo/talk

More information about the talk mailing list