string() Function  
Returns the string value of the argument.
 
Inputs

An object. The object is converted to a string, as described in the following subsection.

 
Output

A string. The input argument is converted to a string as follows:

    If the argument is a node-set, the first node in the node-set is converted to a string. (The first node in the node-set is the one that occurs first in document order.)

    If the argument is a number, it is converted to a string as follows:

      The value NaN is converted to the string "NaN".

      Positive zero is converted to the string "0".

      Negative zero is converted to the string "0".

      Positive infinity is converted to the string "Infinity".

      Negative infinity is converted to the string "-Infinity".

      An integer is converted to a string representing that integer, using no decimal point and no leading zeros. If the integer is negative, it will be preceded by a minus sign (-).

      Any other number is converted to a string with a decimal point, at least one number before the decimal point, and at least one number after the decimal point. If the number is negative, it will be preceded by a minus sign (-). There will not be any leading zeros before the decimal point (with the possible exception of the one required digit before the decimal point). After the decimal point, there will be only as many digits as needed to distinguish this number from all other numeric values defined by the IEEE 754 standard, the same standard used by the Java float and double types.

    If the argument is a boolean value, the value true is represented by the string "true" and the value false is represented by the string "false".

    If the argument is any other type, it is converted to a string in a way that depends on that type. See the documentation for your XSLT processor to find out what other types are supported and how they are converted to strings.

 
Defined in

XPath section 4.2, String Functions.

 
Example

Here is the XML document we'll use to test the string() function:

<?xml version="1.0"?>
<test>
  <p>This is a test XML document used by several 
  of our sample stylesheets.</p>
  <question>
    <text>When completed, the Eiffel Tower was the 
    tallest building in the world.</text>
    <true>You're correct!  The Eiffel Tower was the 
    world's tallest building until 1930.</true>
    <false>No, the Eiffel Tower was the world's 
    tallest building for over 30 years.</false>
  </question>
  <question>
    <text>New York's Empire State Building knocked 
    the Eiffel Tower from its pedestal.</text>
    <true>No, that's not correct.</true>
    <false>Correct!  New York's Chrysler Building, 
    completed in 1930, became the world's tallest.</false>
  </question>
</test>

We'll test the string() function with a variety of arguments:

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <xsl:output method="text"/>

  <xsl:variable name="newline">
<xsl:text>
</xsl:text>
  </xsl:variable>

  <xsl:template match="/">
    <xsl:value-of select="$newline"/>
    <xsl:text>Tests of the string() function:</xsl:text>


    <xsl:value-of select="$newline"/>
    <xsl:value-of select="$newline"/>
    <xsl:text>   string(count(/test))=</xsl:text>
    <xsl:value-of select="string(count(/test))"/>
    <xsl:value-of select="$newline"/>
    <xsl:text>   string(count(/test/question))=</xsl:text>
    <xsl:value-of select="string(count(/test/question))"/>
    <xsl:value-of select="$newline"/>
    <xsl:text>   string('4')=</xsl:text>
    <xsl:value-of select="string('4')"/>
    <xsl:value-of select="$newline"/>
    <xsl:text>   string(true())=</xsl:text>
    <xsl:value-of select="string(true())"/>
    <xsl:value-of select="$newline"/>
    <xsl:text>   string(false())=</xsl:text>
    <xsl:value-of select="string(false())"/>
    <xsl:value-of select="$newline"/>
    <xsl:text>   string(count(/test/question) > 5)=</xsl:text>
    <xsl:value-of select="string(count(/test/question) > 5)"/>
    <xsl:value-of select="$newline"/>
    <xsl:value-of select="$newline"/>
    <xsl:text>Here are the string values of some <text> elements:</xsl:text>
    <xsl:value-of select="$newline"/>
    <xsl:for-each select="/test/question/text">
      <xsl:text>   </xsl:text>
      <xsl:value-of select="string(.)"/>
      <xsl:value-of select="$newline"/>
    </xsl:for-each>
  </xsl:template>

</xsl:stylesheet>

Here are the results of our stylesheet:


Tests of the string() function:

   string(count(/test))=1
   string(count(/test/question))=2
   string('4')=4
   string(true())=true
   string(false())=false
   string(count(/test/question) > 5)=false

Here are the string values of some <text> elements:
   When completed, the Eiffel Tower was the tallest building in the world.
   New York's Empire State Building knocked the Eiffel Tower from its pedestal.