fn:compare

Compare two strings.

Top of page

Related Syntax Constructs

The following construct(s) refer to this construct:


Syntax

fn:compare(string $string1, string? $string2) => integer
fn:compare(string $string1, string? $string2, string $collation) => integer

Description

This function compares two strings. If a third parameter is supplied, the strings are compared according to the specified collation. If the third parameter is not supplied, the strings are compared according to the default collation. It returns one of the following integer values:

-1 the value of $string1 is less than that of $string2
0 the value of $string1 is equal to that of $string2
1 the value of $string1 is greater than that of $string2

If no collation is used, Unicode code points are used when comparing.

Arguments

$string1

string value

$string2

string value

$collation

string value $string

Examples

  • Compare two strings that differ in case:

    compare("Tamino", "tamino")

    This query returns -1, since the value of "T" (U+0054) is less than that of "t" (U+0074).

  • Perform a case-insensitive comparison:

    compare("Tamino", "tamino", "collation?strength=secondary")

    This query returns 0, since the values of the strings "Tamino" and "tamino", disregarding upper-/lower-case, are equal. The collation strength "secondary" indicates that primary character attributes, i.e. the base character itself, and secondary attributes, i.e. accents, are considered when performing the comparison, but tertiary and quaternary attributes, which include case information, are ignored. See tsd:strength for further information.

  • Get all patients with the name "Müller":

    for $i in input()/patient/name
    where compare($i/surname, 'Müller', tf:getCollation($i/surname)) = 0
    return $i

    This query returns the name nodes of all patients whose surname contains the string value "Müller" or a string value that is equal to "Müller" using the collation defined for the element surname.