Perform a Case-Insensitive String Comparison without Using Translate()
Page 1 of 2
Written by
Gregory Scot Collins
Thursday, 20 August 2009, 6:48 PM
This article has been tested to work with the following products and versions. No guarantee of compatibility, with or without modification, is offered for products or versions other than those listed.
InfoPath experts, including myself, have long advised that to perform a case-insensitive string comparison you needed to use the translate() function to convert both strings to uppercase or lowercase. The comparison would look something like:
translate(my:String1, "ABCDEFGHIJKLMNOPQRSTUVWXYZ", "abcdefghijklmnopqrstuvwxyz") = translate(my:String2, "ABCDEFGHIJKLMNOPQRSTUVWXYZ", "abcdefghijklmnopqrstuvwxyz")
Complexity
While this works fine in most cases, it is long to type and is prone to errors (both of which can be mitigated by having a template tucked away somewhere that you can copy and paste). Complexity only increases with the need of additional functions before the strings can be compared, such as substring(), string-before(), or string-after(), etc.
International characters
Another potential bug farm occurs when there is the possibility that international characters will be present. Any such characters would need to be explicitly added to the translate() statement.
Alternate function
Microsoft provides developers with a set of extended functions including one named msxsl:compare-string(). The msxsl namespace is actually part of the function name and must be present.
InfoPath defaults to it using this function when you add conditional formatting that compares two dates. The resulting comparison is something like:
msxsl:string-compare(my:Date1, my:Date2) > 0
Advanced InfoPath developers have long known about this function, but its true power remains undocumented. I recently discovered it while pondering whether there was another version of this function that included the ability to do a case-insensitive comparison.
In my searching, I found a comparable function named ms:string-compare() which includes two optional parameters: one for a language and one for comparison options that makes it possible to perform case-insensitive comparisons. A quick test revealed that msxsl:string-compare() also supports these two undocumented optional parameters; and it even works back in InfoPath 2003!
Stumble It!
Digg It!
del.icio.us




