Description
The fn:compare function returns one of the values:
- -1 if
$comparand1 is less than $comparand2
- 0 if
$comparand1 is equal to $comparand2
- 1 if
$comparand1 is greater than $comparand2
A comparand is greater than the other comparand if it starts with the other comparand and has additional characters. For example, abc is greater than ab .
Comparison operators (=, != , <, <= , >, >= ) can also be used to compare strings, and you may find the syntax more convenient. However, if you need to use a specific collation other than the default, you must use the fn:compare function.
This description is © Copyright 2007, Priscilla Walmsley. It is excerpted from the book XQuery by Priscilla Walmsley, O'Reilly, 2007. For a complete explanation of this function, please refer to Appendix A of the book. Arguments and Return TypeName | Type | Description |
$comparand1 |
xs:string? |
the first string to compare |
$comparand2 |
xs:string? |
the second string to compare |
$collation |
xs:string |
the collation to use for sorting |
return value |
xs:integer? |
ExamplesXPath Example | Results | Explanation |
---|
These examples assume that no default collation is specified. |
compare('a', 'b') |
-1 |
compare('a', 'a') |
0 |
compare('b', 'a') |
1 |
compare('ab', 'abc') |
-1 |
compare('a', 'B') |
1 |
This shows that when using the simple code point collation, a lowercase a comes after an uppercase B . |
compare(upper-case('a'), upper-case('B')) |
-1 |
compare('a', ()) |
() |
compare('Strasse', 'Straße',
'http://datypic.com/german') |
0 |
if the collation equates the ß character with two s's |
compare('a', 'b', 'FOO') |
Error FOCH0002 |
See AlsoHistory |
Recommended Reading:
|