O'Reilly logo

XSLT, 2nd Edition by Doug Tidwell

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Name

[2.0] compare()

Compares two xs:strings and returns -1, 0, or 1, depending on whether the first string is less than, equal to, or greater than the second.

Syntax

xs:integer? compare(xs:string?, xs:string?)
xs:integer? compare(xs:string?, xs:string?, $collation as xs:string)

Inputs

Two xs:strings and an optional collation.

Output

The numeric value -1, 0, or 1, depending on whether the first string is less than, equal to, or greater than the second. The optional $collation argument names a collation used for comparing the two strings. To quote an example from the XSLT 2.0 spec, in a collation for German characters, Strasse and Straße are equal; in other collations they are not. If either string is the empty sequence, compare() returns the empty sequence.

Defined in

XQuery 1.0 and XPath 2.0 Functions and Operators section 7.3, “Equality and Comparison of Strings.”

Example

Here’s a stylesheet that uses compare() in several different ways:

<?xml version="1.0"?>
<!-- compare1.xsl --> <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="text"/> <xsl:template match="/"> <xsl:text>&#xA;Here are some tests of the compare() </xsl:text> <xsl:text>function:&#xA;</xsl:text> <xsl:text>&#xA; compare('Lily', 'lily') = </xsl:text> <xsl:value-of select="compare('Lily', 'lily')"/> <xsl:text>&#xA; compare('Lily', 'Lily') = </xsl:text> <xsl:value-of select="compare('Lily', 'Lily')"/> <xsl:text>&#xA; compare('Lily', </xsl:text> <xsl:text>'&amp;#x4C;&amp;#x69;&amp;#x6C;&amp;#x79;') ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required