home contribute faq download

FunctX XSLT Functions

fn:compare

Compares two strings

Google
Webxsltfunctions.com

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 Type

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

Examples

XSLT ExampleResultsExplanation
These examples assume that no default collation is specified.
<compare('a', 'b'>
<-1>1
<compare('a', 'a'>
<0>
<compare('b', 'a'>
<1>
<compare('ab', 'abc'>
<-1>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>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 Also

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

History

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:

XQuery