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]])
<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).
option | description |
---|---|
u | Makes the comparison case-sensitive, uppercase first. |
i | 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 | -1 |
x = y | 0 |
x > y | 1 |
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"?> <string-sample> <string>1</string> <string>2</string> <string>3</string> </string-sample>
XSLT File (translate.xsl)
<?xml version='1.0'?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ms="urn:schemas-microsoft-com:xslt"> <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')"/>, </xsl:for-each> </xsl:template>
The results of the comparisons are as follows:
x, y | Result |
---|---|
"1" , "2" | -1 |
"2" , "2" | 0 |
"3" , "2" | 1 |