Compartilhar via


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]])

Parameters

  • 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

    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.)

Return values

Returns one of the following three numerical values.


If The example function returns

x < y

-1

x = y

0

x > y

1

Remarks

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.

Example

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>
</xsl:stylesheet>

The results of the comparisons are as follows:

x, y Result

"1" , "2"

-1

"2" , "2"

0

"3" , "2"

1

See Also

Reference

XML Schemas (XSD) Reference
XML Data Types Reference