home contribute faq download

FunctX XSLT Functions


Compares two strings



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 Type

$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?


XPath ExampleResultsExplanation
These examples assume that no default collation is specified.
compare('a', 'b')
compare('a', 'a')
compare('b', 'a')
compare('ab', 'abc')
compare('a', 'B')
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'))
compare('a', ())
compare('Strasse', 'Straße',
if the collation equates the ß character with two s's
compare('a', 'b', 'FOO')
Error FOCH0002

See Also

fn:codepoint-equalWhether two strings have the same Unicode code points


Published OnLast UpdatedContributor(s)
2006-06-272007-02-26W3C, XQuery 1.0 and XPath 2.0 Functions and Operators, http://www.w3.org/TR/xpath-functions/
Datypic XSLT Services

Recommended Reading: