[xquery-talk] Problems with string testing in if

Sjur Nørstebø Moshagen sjur.moshagen at kolumbus.fi
Tue Jun 1 10:37:50 PDT 2004

På 1. jun. 2004 kl. 10.50 skrev Jason Hunter:

> Sjur Nørstebø Moshagen wrote:
>> Hello,
>> I'm new to this list, my apologies if this posting is outside the 
>> intended scope.
> It's right within scope.


> Try:
> if ($lang = "no" or $lang = "nn" or $lang = "nb") then "nor"
> else ...

See below

På 1. jun. 2004 kl. 01.49 skrev Michael Kay:

> This construct:
> if ($lang = "no" or "nn" or "nb")
> will always be true. It means
> if (($lang = "no") or boolean("nn") or boolean("nb"))
> The effective boolean value of "nn" is true, so the whole expression is
> true.

Thanks for the explanation.

> You want:
> if ($lang = ("no", "nn", "nb"))

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.

I have attached the updated function below, for reference.


(: 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,'_')
		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 
(:		else if ($lang = ("sme", "sma", "smn",
					    "smj", "sms", "sme",
					    "nor", "swe", "fin", "eng") ) then $lang :)
		else "test"

More information about the talk mailing list