Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
ms:string-compare Function

ms:string-compare Function

Performs string comparison. This function is necessary because two strings cannot be compared lexicographically using XPath.

          number ms:string-compare(string x, string y, [,string language _
                                             [,string options]])

string x
String value for comparison. Nonstring values are converted to a string value before comparison.

string y
String value for comparison. Nonstring values are converted to a string value before comparison.

string language[optional]
Defines the language to determine the sort order. This is similar to the <xsl:sort> lang attribute (for example, "en-US" or "fr-CA"). If this parameter is omitted or an empty string, the language is defined by the system environment (language of the current thread).

string options[optional]
By default, comparison is case-sensitive, with lowercase first.


option description


Makes the comparison case-sensitive, uppercase first.


Makes the comparison case-insensitive to all traditional differences between characters, such as case, kana, and width. (Character width only applies to some languages.)

Returns one of the following three numerical values.


If The example function returns

x < y


x = y


x > y


Comparisons between strings are made in lexicographical (dictionary) order. Specifying an unsupported language or option causes a runtime error to occur. For MSXML components, E_FAIL is returned.

The following code example shows how to compare two strings by using the ms:string-compare function. The example compares three strings of numbers ("1", "2", and "3") with "2". The results of the comparisons are shown in the table that follows the code.

XML File

<?xml version="1.0"?>

XSLT File (translate.xsl)

<?xml version='1.0'?>
<xsl:stylesheet  version="1.0"
   <xsl:output method="html" omit-xml-declaration="yes"/>
   <xsl:template match="/string-sample">
      <xsl:for-each select="string">
         <xsl:value-of select="ms:string-compare(., '2', 'en-US', 'i')"/>,

The results of the comparisons are as follows:


x, y Result

"1" , "2"


"2" , "2"


"3" , "2"


Build Date:

© 2015 Microsoft